[英]How to color Expander in a DataGrid based on a DataTrigger?
我在基于DataTrigger
为DataGrid
中的Expander
着色时遇到问题。 我尝试了很多东西,也尝试了很多线程,但没有运气。
我希望颜色根据ItemsSource
中的 boolean 进行更改。
下面是设置ItemsSource
的代码:
private void SetDataGrid(ObservableCollection<SourceInfo> myinfoList)
{
var ColectList = new ListCollectionView(myinfoList);
ColectList.GroupDescriptions.Add(new PropertyGroupDescription("DrawNr"));
MyDataGrid.ItemsSource = ColectList;
}
下面是我的 XAML( check
是我在 class 中的参数,即 itemsource):
<DataGrid ItemsSource="{Binding}"
Name="MyDataGrid"
Margin="244,10,20,7"
AutoGenerateColumns="True"
CanUserAddRows="False"
RowEditEnding="MyDataGrid_RowEditEnding"
Loaded="MyDataGrid_Loaded"
BorderBrush="{x:Null}"
Background="{x:Null}"
HorizontalGridLinesBrush="#FF646464"
VerticalGridLinesBrush="#FF646464"
FontFamily="Open Sans">
<DataGrid.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Margin"
Value="0,0,0,5" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="False"
BorderBrush="#FF002255"
Foreground="Black"
BorderThickness="1,1,1,5">
<Expander.Style>
<Style TargetType="{x:Type Expander}">
<Setter Property="Background"
Value="Red" />
<Style.Triggers>
<DataTrigger Binding="{Binding Path=check, RelativeSource={RelativeSource self}}"
Value="True">
<Setter Property="Background"
Value="Green" />
</DataTrigger>
</Style.Triggers>
</Style>
</Expander.Style>
<Expander.Header>
<StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding DrawNr}" />
<TextBlock Text="{Binding ItemCount, StringFormat=Count: {0}}"
Margin="30,0,0,0" />
</StackPanel>
</StackPanel>
</Expander.Header>
<Expander.Content>
<ItemsPresenter />
</Expander.Content>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</DataGrid.GroupStyle>
</DataGrid>
DataGrid
:
对集合视图进行分组时,组的数据上下文是CollectionViewGroup
。 它公开了几个属性,如ItemCount
或Name
,这是您为当前组分组所依据的属性的值。 因此,如果要将Expander
header 绑定到DrawNr
,请使用Name
。
<Expander.Header>
<StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" />
<TextBlock Margin="30,0,0,0" Text="{Binding ItemCount, StringFormat=Count: {0}}" />
</StackPanel>
</StackPanel>
</Expander.Header>
至于check
属性,一个组可以包含多个item ,那么DataTrigger
到底应该考虑哪个item的check
属性,第一个,a boolean 而在所有item中,还有什么?
在这里,我检查组中是否只有一项并使用其check
属性。 如果为True
, Expander
背景将为绿色。 在所有其他情况下(也适用于不止一项),它将是红色的。
<Expander.Style>
<Style TargetType="{x:Type Expander}">
<Setter Property="Background" Value="Red" />
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Items.Count}" Value="1" />
<Condition Binding="{Binding Items[0].Check}" Value="True" />
</MultiDataTrigger.Conditions>
<Setter Property="Background" Value="Green" />
</MultiDataTrigger>
</Style.Triggers>
</Style>
</Expander.Style>
作为一般说明,广泛接受的属性名称约定是 Pascal-Case,例如Check
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.