Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用户代码WPF未处理InvalidOperationException_C#_Wpf_Checkbox_Dispatcher_Ivalueconverter - Fatal编程技术网

C# 用户代码WPF未处理InvalidOperationException

C# 用户代码WPF未处理InvalidOperationException,c#,wpf,checkbox,dispatcher,ivalueconverter,C#,Wpf,Checkbox,Dispatcher,Ivalueconverter,我有一些WPF GUI(托管在WinForm中),它有多个复选框。每个复选框的IsChecked的默认值需要来自一些背景数据中的不同布尔字段。背景数据中的每个布尔字段都映射到可视化树中的复选框。因此,我将每个复选框的IsChecked属性绑定到复选框本身,并使用转换器获取背景数据中相应的布尔值。这样,复选框将成为转换器功能的输入,以便转换器可以知道其在VisualTree中的位置,并在背景数据中查询正确的布尔字段。当用户更改复选框时,选中/未选中的事件处理程序将把值设置回背景数据中的布尔字段。X

我有一些WPF GUI(托管在WinForm中),它有多个
复选框。每个
复选框的
IsChecked
的默认值需要来自一些背景数据中的不同布尔字段。背景数据中的每个布尔字段都映射到
可视化树
中的
复选框
。因此,我将每个
复选框的
IsChecked
属性绑定到
复选框本身,并使用转换器获取背景数据中相应的布尔值。这样,
复选框
将成为转换器功能的输入,以便转换器可以知道其在
VisualTree
中的位置,并在背景数据中查询正确的布尔字段。当用户更改
复选框时,
选中/未选中的事件处理程序将把值设置回背景数据中的布尔字段。XAML是这样的:

    <DataTemplate x:Key="ModuleWithEnableControl">
        <WrapPanel>
            <CheckBox Content="Enable" 
                      IsChecked="{Binding Path=., RelativeSource={RelativeSource Mode=Self}, Mode=OneTime, 
                                  Converter={StaticResource moduleEnableDisableConverter}
                                  }"
                      Checked="ModuleEnabled" Unchecked="ModuleDisabled"
                />
        </WrapPanel>
    </DataTemplate>
class ModuleEnableDisableConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
      System.Windows.Controls.CheckBox theBox = (System.Windows.Controls.CheckBox)value;
      //Here suppose to get the default value of the CheckBox
      //but just return true for now
      return true;
    }
}
    private void ModuleEnabled(object sender, RoutedEventArgs e)
    {
        MessageBoxResult result = MessageBox.Show("Enabled", "Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Question);
        if (result == MessageBoxResult.Yes)
        {
          //Check the sender (CheckBox)'s location in the VisualTree,
          //Do set the boolean field in the background data
        }
        return;
    }
选中的处理程序代码如下所示:

    <DataTemplate x:Key="ModuleWithEnableControl">
        <WrapPanel>
            <CheckBox Content="Enable" 
                      IsChecked="{Binding Path=., RelativeSource={RelativeSource Mode=Self}, Mode=OneTime, 
                                  Converter={StaticResource moduleEnableDisableConverter}
                                  }"
                      Checked="ModuleEnabled" Unchecked="ModuleDisabled"
                />
        </WrapPanel>
    </DataTemplate>
class ModuleEnableDisableConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
      System.Windows.Controls.CheckBox theBox = (System.Windows.Controls.CheckBox)value;
      //Here suppose to get the default value of the CheckBox
      //but just return true for now
      return true;
    }
}
    private void ModuleEnabled(object sender, RoutedEventArgs e)
    {
        MessageBoxResult result = MessageBox.Show("Enabled", "Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Question);
        if (result == MessageBoxResult.Yes)
        {
          //Check the sender (CheckBox)'s location in the VisualTree,
          //Do set the boolean field in the background data
        }
        return;
    }
当程序启动时,转换器首先运行,因为它返回true,然后立即运行选中的处理程序
ModuleEnabled
。然后在处理程序中执行
MessageBox.Show()
时出现问题,一个
弹出窗口
抱怨:

用户代码未处理InvalidOperationException

调度程序处理已暂停

但消息仍在处理中

如果我注释掉
MessageBox.Show()
行,程序将按预期执行。如果我没有将
IsChecked
绑定到
复选框
本身,也没有问题,当用户更改
复选框时,
消息框
会完美显示。我想知道为什么事件处理程序中的
MessageBox.Show()
与绑定转换器冲突?另外,在使用绑定设置初始值时,是否有方法不触发选中的处理程序