我正在使用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作为示例以了解有关转换器的信息是错误的? 就像我做得很好一样。
答案 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,出于明显的原因,它容易出现运行时错误,并且不存在类型检查。
我可以想到两种解决方案:
在此视图模型示例中,您可以将视图各自的属性(IsPlaying
,IsStopped
,IsPaused
绑定到视图中的每个复选框:
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; }
}
DataTrigger
用于PlayState属性上的复选框。然后,您可以检查键入的枚举值而不是字符串文字。