简体   繁体   中英

Inverted bool to radio button from dataContext to userControl

How can I bind an inverted value of a bool in my 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. 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. 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

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

where my project is the namespace in which the value converter class is defined

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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