我想将
enum
绑定到 WinUI3
中的一些单选按钮,我尝试了 Wpf
中使用的几种解决方案,但它们都无法在 WinUI3
中运行
这是我最后的尝试:
// My actual enum
public enum PPGSensorType
{
Reflective = 1,
Transmission = 2
}
public class EnumToBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
string parameterString = parameter as string;
if (parameterString == null)
return false;
if (Enum.IsDefined(value.GetType(), value) == false)
return false;
object parameterValue = Enum.Parse(value.GetType(), parameterString);
return parameterValue.Equals(value);
}
// TargetType is `Object` in ConvertBack. I don't know what to do about it.
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
string parameterString = parameter as string;
if (parameterString == null)
throw new ArgumentException("Parameter should be passed for parsing the enum value.");
return Enum.Parse(targetType, parameterString);
}
}
正如我在上面提到的,
TargetType
是转换回来的Object
,所以它无法正确转换回值。
<RadioButtons>
<RadioButton Margin="20 0 0 0" IsChecked="{Binding ElementName=addDeviceDialog, Path=ViewModel.Device.PPGSensorType, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter=Reflective,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Content="Reflective"/>
<RadioButton Margin="20 0 0 0" IsChecked="{Binding ElementName=addDeviceDialog, Path=ViewModel.Device.PPGSensorType, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter=Transmission, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Content="Transmission"/>
</RadioButtons>
我尝试了一种使用静态资源来告知 Converter 参数的确切值的新方法。在
app.xaml
中,我添加了以下资源:
<presentation:PPGSensorType x:Key="ReflectivePPGSensor">Reflective</presentation:PPGSensorType>
<presentation:PPGSensorType x:Key="TransmissionPPGSensor">Transmission</presentation:PPGSensorType>
<RadioButtons>
<RadioButton Margin="20 0 0 0" IsChecked="{Binding ElementName=addDeviceDialog, Path=ViewModel.Device.PPGSensorType, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={StaticResource ReflectivePPGSensor},Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Content="Reflective"/>
<RadioButton Margin="20 0 0 0" IsChecked="{Binding ElementName=addDeviceDialog, Path=ViewModel.Device.PPGSensorType, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={StaticResource TransmissionPPGSensor}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Content="Transmission"/>
</RadioButtons>
它也不起作用。
这应该有效:
MainPageViewModel.cs
public partial class MainPageViewModel : ObservableObject
{
[ObservableProperty]
private List<Color> _colors = Enum.GetValues<Color>().ToList();
[ObservableProperty]
private Color? _selectedColor = Color.Red;
partial void OnSelectedColorChanged(Color? oldValue, Color? newValue)
{
System.Diagnostics.Debug.WriteLine($"SelectedColor changed from {oldValue} to {newValue}");
}
}
主页.xaml
<RadioButtons
ItemsSource="{x:Bind ViewModel.Colors, Mode=OneWay}"
SelectedItem="{x:Bind ViewModel.SelectedColor, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />