RadioButton, Binding, Converters[英] RadioButton, Binding, Converters

本文是小编为大家收集整理的关于RadioButton, Binding, Converters的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

本文来自:IT宝库(https://www.itbaoku.cn)

我正在从事Visual Studio 2008,而RadioButtons有问题.

我有3个radiobutton:

   <Window.Resources>
      // [...]
   <DataTemplate x:Key="gridViewReadyTemplate">
        <StackPanel>
            <RadioButton GroupName="{Binding IdCommand}" IsChecked="{Binding CommandState, Mode=TwoWay, Converter={StaticResource enumBooleanConverter}, ConverterParameter=ready}" />
        </StackPanel>
    </DataTemplate>

    <DataTemplate x:Key="gridViewReportedTemplate">
        <StackPanel>
            <RadioButton GroupName="{Binding IdCommand}" IsChecked="{Binding CommandState, Mode=TwoWay, Converter={StaticResource enumBooleanConverter}, ConverterParameter=reported}" />
        </StackPanel>
    </DataTemplate>

    <DataTemplate x:Key="gridViewCanceledTemplate">
        <StackPanel>
            <RadioButton GroupName="{Binding IdCommand}" IsChecked="{Binding CommandState, Mode=TwoWay, Converter={StaticResource enumBooleanConverter}, ConverterParameter=canceled}" />
        </StackPanel>
    </DataTemplate>

    // [...]


   <ListView Margin="82,133.32,342.5,0" Name="listView1" ItemsSource="{Binding CurrentTrain.PSCommandCollection, Mode=TwoWay}" Height="111.25" VerticalAlignment="Top">
        <ListView.View>
            <GridView>
                // [...]
                <GridViewColumn Header="Préparé" Width="50" CellTemplate="{StaticResource gridViewReadyTemplate }" />
                <GridViewColumn Header="Reporté" Width="50" CellTemplate="{StaticResource gridViewReportedTemplate }" />
                <GridViewColumn Header="Annulé" Width="50" CellTemplate="{StaticResource gridViewCanceledTemplate }" />
            </GridView>
        </ListView.View>
    </ListView>

转换器:

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    string param = (string)parameter;
    Enumerators.State state = (Enumerators.State)value;

    switch (param)
    {
        case "ready":
            if (state == Enumerators.State.READY)
                return true;
            return false;
        case "reported":
            if (state == Enumerators.State.REPORTED)
                return true;
            return false;
        case "canceled":
            if (state == Enumerators.State.CANCELED)
                return true;
            return false;
    }
    return false;
}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    string param = (string)parameter;

    if ((bool?)value == true)
    {
        switch (param)
        {
            case "ready":
                return Enumerators.State.READY;
            case "reported":
                return Enumerators.State.REPORTED;
            case "canceled":
                return Enumerators.State.CANCELED;
        }
    }
    return Enumerators.State.NONE;
}

以及radiobutton绑定的属性:

private Enumerators.State commandState;
public Enumerators.State CommandState
{
    get { return commandState; }
    set
    {
        if (commandState != value)
        {
            commandState = value;
            NotifyPropertyChanged("CommandState");
        }
        else
        {
            commandState = Enumerators.State.NONE;
            NotifyPropertyChanged("CommandState");
        }
    }
}

当我单击RadioButton时,状态会发生良好变化. 问题是当我想通过单击它来取消选中radiobutton时,状态仍会改变,但是RadioButton仍在检查中.

我将断点放在转换器中,函数转换.例如,如果我想取消选中"准备就绪",则该程序为2次,用于"报告"和"取消",但不是为"准备就绪" ...

我真的不明白问题在哪里. 你能解释一下我如何修复它吗?

推荐答案

这是RadioButton的问题.一旦不受限制,它就会失去其绑定.为了解决此问题,您可以将radiobuttons的RadioButton.Command绑定到viewModel的命令,然后发送唯一的命令参数以识别CommandHandler中的命令.

<RadioButton Command="{Binding MyCommand}" CommandParameter="Radio1"/>
<RadioButton Command="{Binding MyCommand}" CommandParameter="Radio2"/>
<RadioButton Command="{Binding MyCommand}" CommandParameter="Radio3"/>

和命令处理程序中,您可以根据接收到的命令参数而不是在转换器中进行设置.

本文地址:https://www.itbaoku.cn/post/1970332.html

问题描述

I am working on Visual Studio 2008, and I have a problem with radiobuttons.

I have 3 radioButton :

   <Window.Resources>
      // [...]
   <DataTemplate x:Key="gridViewReadyTemplate">
        <StackPanel>
            <RadioButton GroupName="{Binding IdCommand}" IsChecked="{Binding CommandState, Mode=TwoWay, Converter={StaticResource enumBooleanConverter}, ConverterParameter=ready}" />
        </StackPanel>
    </DataTemplate>

    <DataTemplate x:Key="gridViewReportedTemplate">
        <StackPanel>
            <RadioButton GroupName="{Binding IdCommand}" IsChecked="{Binding CommandState, Mode=TwoWay, Converter={StaticResource enumBooleanConverter}, ConverterParameter=reported}" />
        </StackPanel>
    </DataTemplate>

    <DataTemplate x:Key="gridViewCanceledTemplate">
        <StackPanel>
            <RadioButton GroupName="{Binding IdCommand}" IsChecked="{Binding CommandState, Mode=TwoWay, Converter={StaticResource enumBooleanConverter}, ConverterParameter=canceled}" />
        </StackPanel>
    </DataTemplate>

    // [...]


   <ListView Margin="82,133.32,342.5,0" Name="listView1" ItemsSource="{Binding CurrentTrain.PSCommandCollection, Mode=TwoWay}" Height="111.25" VerticalAlignment="Top">
        <ListView.View>
            <GridView>
                // [...]
                <GridViewColumn Header="Préparé" Width="50" CellTemplate="{StaticResource gridViewReadyTemplate }" />
                <GridViewColumn Header="Reporté" Width="50" CellTemplate="{StaticResource gridViewReportedTemplate }" />
                <GridViewColumn Header="Annulé" Width="50" CellTemplate="{StaticResource gridViewCanceledTemplate }" />
            </GridView>
        </ListView.View>
    </ListView>

The converter :

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    string param = (string)parameter;
    Enumerators.State state = (Enumerators.State)value;

    switch (param)
    {
        case "ready":
            if (state == Enumerators.State.READY)
                return true;
            return false;
        case "reported":
            if (state == Enumerators.State.REPORTED)
                return true;
            return false;
        case "canceled":
            if (state == Enumerators.State.CANCELED)
                return true;
            return false;
    }
    return false;
}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    string param = (string)parameter;

    if ((bool?)value == true)
    {
        switch (param)
        {
            case "ready":
                return Enumerators.State.READY;
            case "reported":
                return Enumerators.State.REPORTED;
            case "canceled":
                return Enumerators.State.CANCELED;
        }
    }
    return Enumerators.State.NONE;
}

And the property where radiobutton are binding on :

private Enumerators.State commandState;
public Enumerators.State CommandState
{
    get { return commandState; }
    set
    {
        if (commandState != value)
        {
            commandState = value;
            NotifyPropertyChanged("CommandState");
        }
        else
        {
            commandState = Enumerators.State.NONE;
            NotifyPropertyChanged("CommandState");
        }
    }
}

When I click on a radiobutton, the state is changing well. The problem is when I want to uncheck a radiobutton by clicking on it, the state changes, but the radiobutton still checked.

I put breakpoint in my converter, function Convert. For example, if I want to uncheck "ready", the program go in 2 times, for "reported" and "canceled", but not for "ready"...

I really don't understand where is the problem. Can you explain me how to fix it ?

推荐答案

This is the problem with RadioButton. It loses its binding once it gets unchecked. To fix this, you can bind RadioButton.Command of Radiobuttons to a command of your ViewModel and send a unique CommandParameter to identify which button has called the command in commandhandler.

<RadioButton Command="{Binding MyCommand}" CommandParameter="Radio1"/>
<RadioButton Command="{Binding MyCommand}" CommandParameter="Radio2"/>
<RadioButton Command="{Binding MyCommand}" CommandParameter="Radio3"/>

and in the command handler you can set the property depending on the command parameter received instead of doing it in converter.