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