此枚举添加到您的项目: - :当每次CouldBeXXX
性质改变
private RowState _RowState;
public RowState RowState
{
get { return _RowState; }
set
{
if (value != _RowState)
{
_RowState = value;
NotifyPropertyChanged("RowState"); // Assumes typical implementation of INotifyPropertyChanged
}
}
}
private void UpdateRowState()
{
if (CouldBeWhite)
RowState = RowState.White;
else if (CouldBeGreen)
RowState = RowState.Green;
else if (CouldBeRed)
RowState = RowState.Red;
else
RowState = RowState.Blue;
}
呼叫UpdateRowState -
public enum RowState
{
Blue,
Red,
Green,
White
}
那么这个属性添加到您的视图模型。
考虑一下,你可能没有像前任那样将前景变成白色或红色或绿色。会有一些原因为什么它的白色,红色或绿色。因此,在你的代码中想一个简单的短名称来表示这些原因,并用那些更有意义的名称替换颜色名称。
现在去取StringToValueConverter
的代码blog。这方面的一个实例添加到您的XAML中的UserControl.Resources
:
<local:StringToObjectConverter x:Key="RowStateToBrush">
<ResourceDictionary>
<SolidColorBrush Color="Red" x:Key="Red" />
<SolidColorBrush Color="Green" x:Key="Green" />
<SolidColorBrush Color="White" x:Key="White" />
<SolidColorBrush Color="Blue" x:Key="__default__" />
</ResourceDictionary>
</local:StringToObjectConverter>
可以绑定一个TextBlock
:
<TextBlock Text="{Binding SomeTextProperty}" Foreground="{Binding RowState, Converter={StaticResource RowStateToBrush}}" />
这逻辑在我脑海里燃烧孔。怎么样enum ShouldBe {White,Green,Red}'并且完成。 – Will