简体   繁体   English

将bool倒置为dataContext到userControl的单选按钮

[英]Inverted bool to radio button from dataContext to userControl

How can I bind an inverted value of a bool in my xaml? 如何在xaml中绑定布尔值的倒数?

I know, I know, there are a lot of q&a about this, but there aren't any about a specific case: when the converter is in a view model and the radio button in a user control. 我知道,我对此有很多疑问,但是并没有关于特定情况的信息:转换器处于视图模型中,而单选按钮位于用户控件中。

how can I correctly refer to the main window view model data context? 如何正确引用主窗口视图模型数据上下文? In other case I used the code I've posted, but in this case I don't get how to use it. 在其他情况下,我使用了已发布的代码,但是在这种情况下,我不知道如何使用它。

UPDATE CODE: 更新代码:

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);
            [...]

    }
....
}

ANOTHER CODE UPDATE: 另一个代码更新:

In my xaml, I have a devexpress GridControl where I list my observableCollection from db. 在我的xaml中,我有一个devexpress GridControl,在其中列出了来自db的observableCollection。 When I click one element, a layout group is populated with the relative data. 当我单击一个元素时,布局组将填充相关数据。

In this layout group, I have: 在此布局组中,我具有:

<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>

WhenI change from one record to another, the converter gives me error... why? 当我从一条记录更改为另一条记录时,转换器给我错误...为什么? It fails the "if (targetType != typeof(bool))" row. 它在“ if(targetType!= typeof(bool))”行中失败。 So I tried to change that like: 因此,我尝试将其更改为:

if (value.getType() != typeof(bool))

but in this way, without fails nothing in the code, it put the wrong value in the radiobutton! 但是通过这种方式,在代码中没有失败的情况下,它在单选按钮中输入了错误的值!

You need to make sure that the value converter is referenced in the XAML 您需要确保XAML中引用了值转换器

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

where my project is the namespace in which the value converter class is defined 我的项目是在其中定义值转换器类的名称空间

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM