IValueConverter参数检查

时间:2018-09-24 17:57:43

标签: c# wpf mvvm

我正在使用IValueConverter设置复选框。 但是我有三个复选框,而Im就像RadioButton一样使用。选择一个后,其他的则需要取消选中。

所以我做了类似的事情>

<MenuItem StaysOpenOnClick="True" 
          Header="Reiniciar áudio"
          IsCheckable="True"
          Name="RestartAudioCheck" 
          IsChecked="{Binding PlayType, 
          Mode=TwoWay,
          Converter={StaticResource PlayTypeCheckedConverter},
          ConverterParameter=restart}">
</MenuItem>
<MenuItem StaysOpenOnClick="True" 
          Header="Parar áudio"
          IsCheckable="True"
          Name="StopAudioCheck"
          IsChecked="{Binding PlayType, 
            Mode=TwoWay,
          Converter={StaticResource PlayTypeCheckedConverter},
          ConverterParameter=stop}">
</MenuItem>
<MenuItem StaysOpenOnClick="True" 
          Header="Pausar áudio"
          IsCheckable="True"
          Name="PauseAudioCheck"
          IsChecked="{Binding PlayType, 
            Mode=TwoWay,
          Converter={StaticResource PlayTypeCheckedConverter},
          ConverterParameter=pause}">
</MenuItem>

和转换器:

class PlayTypeCheckedConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (!(value is PlayType type))
            return false;

        if (!(parameter is string check))
            return false;

        if (check.CompareTo("restart") == 0 && type == PlayType.Restart)
            return true;
        if (check.CompareTo("stop") == 0 && type == PlayType.Stop)
            return true;
        if (check.CompareTo("pause") == 0 && type == PlayType.Pause)
            return true;

        return false;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (!(value is bool isChecked))
            return true;

        if (!(parameter is string type))
            return PlayType.Restart;

        if (type.CompareTo("restart") == 0 && isChecked)
            return PlayType.Restart;
        if (type.CompareTo("stop") == 0 && isChecked)
            return PlayType.Stop;
        if (type.CompareTo("pause") == 0 && isChecked)
            return PlayType.Pause;

        return PlayType.Restart;
    }
}

我的问题是:传递一个类似绝对文本的ConverterParameter作为示例以了解有关转换器的信息是错误的? 就像我做得很好一样。

2 个答案:

答案 0 :(得分:3)

对于任何属性类型,都可以有一个相等转换器。使用它的技巧是传递特定类型的参数,而不是字符串表示形式。

转换器:

public class EqualityCheckConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return Equals(value, parameter);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (Equals(value, true))
            return parameter;

        return Binding.DoNothing;
    }
}

enum的示例:

// property to bind
public DayOfWeek Day { get; set; }

绑定:

<Window.Resources>
    <wpfDemos:EqualityCheckConverter x:Key="EqualCheckedConverter"/>
</Window.Resources>

<Menu>
    <MenuItem StaysOpenOnClick="True" 
              Header="Tuesday"
              IsCheckable="True"
              IsChecked="{Binding Day, 
              Mode=TwoWay,
              Converter={StaticResource EqualCheckedConverter},
              ConverterParameter={x:Static system:DayOfWeek.Tuesday}}"/>

    <MenuItem StaysOpenOnClick="True" 
              Header="Wednesday"
              IsCheckable="True"
              IsChecked="{Binding Day, 
              Mode=TwoWay,
              Converter={StaticResource EqualCheckedConverter},
              ConverterParameter={x:Static system:DayOfWeek.Wednesday}}"/>

    <MenuItem StaysOpenOnClick="True" 
              Header="Thursday"
              IsCheckable="True"
              IsChecked="{Binding Day, 
              Mode=TwoWay,
              Converter={StaticResource EqualCheckedConverter},
              ConverterParameter={x:Static system:DayOfWeek.Thursday}}"/>
</Menu>

int

的示例
public int TestNum { get; set; }
<Window.Resources>
    <wpfDemos:EqualityCheckConverter x:Key="EqualCheckedConverter"/>
    <system:Int32 x:Key="Zero">0</system:Int32>
    <system:Int32 x:Key="Ten">10</system:Int32>
</Window.Resources>
<Menu>
    <MenuItem StaysOpenOnClick="True" 
              Header="0"
              IsCheckable="True"
              IsChecked="{Binding TestNum, 
              Mode=TwoWay,
              Converter={StaticResource EqualCheckedConverter},
              ConverterParameter={StaticResource Zero}}"/>

    <MenuItem StaysOpenOnClick="True" 
              Header="10"
              IsCheckable="True"
              IsChecked="{Binding TestNum, 
              Mode=TwoWay,
              Converter={StaticResource EqualCheckedConverter},
              ConverterParameter={StaticResource Ten}}"/>
</Menu>

答案 1 :(得分:2)

像您一样,显式比较字符串文字通常被认为是不好的做法。是否使用WPF,出于明显的原因,它容易出现运行时错误,并且不存在类型检查。

我可以想到两种解决方案:

  1. 最常见的方法是在视图模型中添加三个属性,即只要满足状态,则仅使用基本上返回true或false的getter。然后,当“播放状态”更改时,您可以提高所有这三个布尔属性的更改属性。

在此视图模型示例中,您可以将视图各自的属性(IsPlayingIsStoppedIsPaused绑定到视图中的每个复选框:

    private State _playState;
    public State PlayState
    {
        get { return _playState; }
        set {

            SetProperty(ref _playState, value);
            RaisePropertyChanged(nameof(IsPlaying));
            RaisePropertyChanged(nameof(IsPaused));
            RaisePropertyChanged(nameof(IsStopped));
        }
    }

    public bool IsPlaying
    {
        get { return _playState == State.Playing; }
    }
    public bool IsPaused
    {
        get { return _playState == State.Paused; }
    }
    public bool IsStopped
    {
        get { return _playState == State.Stopped; }
    }
  1. 在XAML中将DataTrigger用于PlayState属性上的复选框。然后,您可以检查键入的枚举值而不是字符串文字。