似乎无法让我的 IValueConverter 工作
Can't seem to get my IValueConverter to work
有以下简单的xaml
<UserControl.Resources>
<converters:StateToColorConverter x:Key="stateToColorConverter"/>
</UserControl.Resources>
<StackPanel>
<Grid Width="150" Height="100" Background="{Binding State, Converter={StaticResource stateToColorConverter}}"></Grid>
<Button
Width="100"
Height="70"
Command="{Binding InitializeCommand}">Initialize</Button>
</StackPanel>
它的视图模型有一个 属性 状态,它有一个正确的值。
public class MachineControlViewModel :ViewModelBase
{
private readonly IMachine machine;
public RelayCommand InitializeCommand { get; set; }
private MachineStates state;
public MachineStates State
{
get { return state; }
set { Set(() => State, ref state, value); }
}
public MachineControlViewModel(IMachine machine)
{
this.machine = machine;
InitializeCommand = new RelayCommand(Initialize, CanInitialize);
State = machine.State;
machine.StateChanged += MachineOnStateChanged;
}
// left out irrelevant parts
}
然后,IValueConverter 实现
public class StateToColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var state = (MachineStates) Enum.Parse(typeof (MachineStates), value.ToString());
switch (state)
{
case MachineStates.Idle:
return Color.Red;
case MachineStates.Initialized:
return Color.Green;
case MachineStates.Production:
return Color.Blue;
case MachineStates.Error:
return Color.Red;
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
当我运行我的应用程序时,Grid 的背景颜色不会显示。
当我将 BackColor 设置为硬编码时(因此没有值转换器),它可以正确显示。
当我使用我的值转换器并在转换方法中放置一个断点时,我可以看到代码执行得很好,并且返回了颜色。但是什么都没有显示...
我做错了什么?
转换器应该 return Brush
,而不是 Color
,因为那是 Background
属性.[=16= 的类型]
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
Brush brush = null;
switch ((MachineStates)value)
{
case MachineStates.Idle:
brush = Brushes.Red;
break;
case MachineStates.Initialized:
brush = Brushes.Green;
break;
case MachineStates.Production:
brush = Brushes.Blue;
break;
case MachineStates.Error:
brush = Brushes.Red;
break;
default:
break;
}
return brush;
}
它尤其不应该 return a System.Drawing.Color
(就像你所做的那样),因为那是 WinForms,而不是 WPF。
背景是画笔类型。将转换器更改为此
return new SolidColorBrush(Colors.Red);
有以下简单的xaml
<UserControl.Resources>
<converters:StateToColorConverter x:Key="stateToColorConverter"/>
</UserControl.Resources>
<StackPanel>
<Grid Width="150" Height="100" Background="{Binding State, Converter={StaticResource stateToColorConverter}}"></Grid>
<Button
Width="100"
Height="70"
Command="{Binding InitializeCommand}">Initialize</Button>
</StackPanel>
它的视图模型有一个 属性 状态,它有一个正确的值。
public class MachineControlViewModel :ViewModelBase
{
private readonly IMachine machine;
public RelayCommand InitializeCommand { get; set; }
private MachineStates state;
public MachineStates State
{
get { return state; }
set { Set(() => State, ref state, value); }
}
public MachineControlViewModel(IMachine machine)
{
this.machine = machine;
InitializeCommand = new RelayCommand(Initialize, CanInitialize);
State = machine.State;
machine.StateChanged += MachineOnStateChanged;
}
// left out irrelevant parts
}
然后,IValueConverter 实现
public class StateToColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var state = (MachineStates) Enum.Parse(typeof (MachineStates), value.ToString());
switch (state)
{
case MachineStates.Idle:
return Color.Red;
case MachineStates.Initialized:
return Color.Green;
case MachineStates.Production:
return Color.Blue;
case MachineStates.Error:
return Color.Red;
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
当我运行我的应用程序时,Grid 的背景颜色不会显示。
当我将 BackColor 设置为硬编码时(因此没有值转换器),它可以正确显示。
当我使用我的值转换器并在转换方法中放置一个断点时,我可以看到代码执行得很好,并且返回了颜色。但是什么都没有显示...
我做错了什么?
转换器应该 return Brush
,而不是 Color
,因为那是 Background
属性.[=16= 的类型]
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
Brush brush = null;
switch ((MachineStates)value)
{
case MachineStates.Idle:
brush = Brushes.Red;
break;
case MachineStates.Initialized:
brush = Brushes.Green;
break;
case MachineStates.Production:
brush = Brushes.Blue;
break;
case MachineStates.Error:
brush = Brushes.Red;
break;
default:
break;
}
return brush;
}
它尤其不应该 return a System.Drawing.Color
(就像你所做的那样),因为那是 WinForms,而不是 WPF。
背景是画笔类型。将转换器更改为此
return new SolidColorBrush(Colors.Red);