Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.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# 从dataContext到userControl的反向bool到单选按钮_C#_Wpf_Mvvm_Binding_Converter - Fatal编程技术网

C# 从dataContext到userControl的反向bool到单选按钮

C# 从dataContext到userControl的反向bool到单选按钮,c#,wpf,mvvm,binding,converter,C#,Wpf,Mvvm,Binding,Converter,如何在xaml中绑定布尔的倒过来的值 我知道,我知道,关于这一点有很多问答,但没有任何关于特定情况的问答:当转换器位于视图模型中,而单选按钮位于用户控件中时 如何正确引用主窗口视图模型数据上下文?在其他情况下,我使用了我发布的代码,但在这种情况下,我不知道如何使用它 更新代码: namespace ***.ViewModel { [ValueConversion(typeof(bool), typeof(bool))] public class InverseBooleanCon

如何在xaml中绑定布尔的倒过来的值

我知道,我知道,关于这一点有很多问答,但没有任何关于特定情况的问答:当转换器位于视图模型中,而单选按钮位于用户控件中时

如何正确引用主窗口视图模型数据上下文?在其他情况下,我使用了我发布的代码,但在这种情况下,我不知道如何使用它

更新代码:

namespace ***.ViewModel
{
    [ValueConversion(typeof(bool), typeof(bool))]
    public class InverseBooleanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            if (targetType != typeof(bool))
                throw new InvalidOperationException("The target must be a boolean");
            return !(bool)value;
        }
        public object ConvertBack(object value, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
        class MainWindowViewModel : MyClass.UtilityClasses.ViewModelBase
    {
        public MainWindowViewModel(){
            SaveCommand = new DelegateCommand(SaveData, CanSave );
            UndoCommand = new DelegateCommand(UndoActions, CanSave);
            [...]

    }
....
}
另一个代码更新:

在我的xaml中,我有一个devexpress GridControl,其中列出了我从db中的observableCollection。单击一个元素时,布局组将填充相关数据

在此布局组中,我有:

<StackPanel Margin="0" Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Left">
    <RadioButton Content="{DynamicResource Locale}" Margin="10,0,0,0" x:Name="rd_LOCALE" VerticalAlignment="Center" IsChecked="{Binding Path=REMOTO, Converter={StaticResource InverseBooleanConverter}}" GroupName="Location" Panel.ZIndex="9" TabIndex="10" />
    <RadioButton Content="{DynamicResource Remoto}" Margin="10,0,6,0" x:Name="rd_REMOTO" VerticalAlignment="Center" IsChecked="{Binding REMOTO}" GroupName="Location" Panel.ZIndex="10" TabIndex="11" Tag="PRISMA" />
</StackPanel>

但是在这种情况下,如果代码中没有任何失败,它就会在radiobutton中输入错误的值

您需要确保值转换器在XAML中被引用

<UserControl.Resources>
    <myproject:InverseBooleanConverter x:Key="InverseBooleanConverter" />
</UserControl.Resources>


其中,我的项目是定义值转换器类的名称空间

当然可以使用值转换器来反转布尔值?这就是我正在尝试的!)还有其他想法吗?哦,我刚刚看到了对不起!您的转换器是否在“控制资源”部分中定义?它是在MainWindowViewModel.cs中定义的。单选按钮位于MainWindowView.xaml中包含的用户控件中。我从另一个问题复制了转换器。InverseBoleAnConverter是在MainWindowViewModel类中定义的类吗?在同一命名空间中键入创建新类文件时,然后将值转换器代码复制到该类中,(确保删除现有的公共类Class1代码)然后尝试在“if(targetType!=typeof(bool))”行上放置一个断点,并确保目标类型在传递给Convertermy huess时实际上是一个布尔值,因为属性[ValueConversion(typeof(bool),typeof(bool))]会把事情搞砸,我从来都不需要这些属性,所以不确定为什么需要它们
<UserControl.Resources>
    <myproject:InverseBooleanConverter x:Key="InverseBooleanConverter" />
</UserControl.Resources>