我有这个视图模型:
public class MyData
{
public string Status;
public StatusMsg StatusMessage;
private Brush _statusBrushes;
public Brush StatusBrushes
{
get
{
switch (StatusMessage)
{
case StatusMsg.Cancel:
return Brushes.Red;
case StatusMsg.InProcess:
return Brushes.Blue;
case StatusMsg.Done:
return Brushes.Green;
default:
return Brushes.Green;
}
}
set { _statusBrushes = value; }
}
public enum StatusMsg
{
Cancel,
Done,
InProcess,
}
}
现在我有GridViewColumn
:
<GridViewColumn Width="180" Header="Status">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock x:Name="Txt" Text="{Binding Status}" Foreground="Yellow" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
因为您可以看到此GridViewColumn
颜色为黄色,我想根据我的StatusMsg
(我的枚举)更改它,所以我的问题是如何将我的颜色绑定到我的XAML
?
答案 0 :(得分:0)
我建议创建一个IValueConverter
,它能够将您的枚举值转换为适当的颜色,然后您的绑定看起来像:
<GridViewColumn Width="180" Header="Status">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock x:Name="Txt" Text="{Binding Status}"
Foreground="{Binding Path=StatusColor, Converter={StaticResource MyStatusColorConverter}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
您当然还需要创建资源,这是一个快速搜索搜索的教程:http://wpftutorial.net/ValueConverters.html