我在 WPF 中使用 DataGrid 来显示值。 现在我想要绿色和红色的行。我用 DataTrigger 试了一下,但没有任何反应。
我的 XAML:
<DataGrid x:Name="dgAbos" ItemsSource="{Binding Source=AboList}" HorizontalAlignment="Stretch" Margin="10,30,10,10" VerticalAlignment="Stretch" Height="Auto" Width="Auto">
<DataGrid.Columns>
<DataGridTextColumn Header="ItemID" Binding="{Binding ItemID}" />
</DataGrid.Columns>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding Active}" Value="false">
<Setter Property="Background" Value="Red"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding Active}" Value="true">
<Setter Property="Background" Value="Green"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
</DataGrid>
绑定(bind)类型是:
ObservableCollection<OPCItem> AboList = new ObservableCollection<OPCItem>();
要显示的项目是 OPCItem:
class OPCItem
{
public String ItemID { get; set; }
public String Name { get; set; }
public String Value { get; set; }
public DateTime DateTime { get; set; }
public String Group { get; set; }
private Boolean _Active;
public String Active
{
get
{
return (_Active == true ? "Aktiv" : "Inaktiv");
}
set
{
_Active = Convert.ToBoolean(value);
}
}
}
我如何填写列表:
AboList.Add(new OPCItem { ItemID = Item.ItemID, Group = GroupName, Active = "true" });
但是行没有改变颜色,为什么?
最佳答案
您的 Active
属性的值永远不会是 "true"
或 "false"
,因此触发器永远不会被实际触发。您应该修改触发器的预期值以反射(reflect) Active
可能采用的值(即 "Aktiv"
和 "Inaktiv"
):
<DataTrigger Binding="{Binding Active}" Value="Inaktiv">
<Setter Property="Background" Value="Red"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding Active}" Value="Aktiv">
<Setter Property="Background" Value="Green"></Setter>
</DataTrigger>
关于c# - WPF DataGrid 通过绑定(bind)设置样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38054858/