c# - 如何在基于 DataTrigger 的 DataGrid 中为 Expander 着色?
问题描述
我Expander
在DataGrid
基于DataTrigger
. 我已经尝试了很多东西并红色了很多线程,但没有运气。
我希望颜色根据ItemsSource
.
下面是设置的代码ItemsSource
:
private void SetDataGrid(ObservableCollection<SourceInfo> myinfoList)
{
var ColectList = new ListCollectionView(myinfoList);
ColectList.GroupDescriptions.Add(new PropertyGroupDescription("DrawNr"));
MyDataGrid.ItemsSource = ColectList;
}
下面是我的 XAML(这check
是我在 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
or Name
,它是您为当前组分组的属性的值。因此,如果要将Expander
标头绑定到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
属性,一个组可以包含多个项目,那么check
究竟应该考虑哪个项目的属性DataTrigger
,第一个,布尔值和所有项目,还有什么?
在这里,我检查组中是否只有一个项目并使用它的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
.
推荐阅读
- group-policy - 计算机中的标准用户
- javascript - Html web worker 没有按预期工作
- angular - FormData 未进入 API 控制器的 Post 方法
- php - 带 GPS 路线的数据库
- google-app-maker - 在 Appmaker 中显示数据源总页数的最佳方式
- php - 内联 CSS 不适用于 Mpdf
- ios - tableView 滚动到最新单元格的底部 - scrollToRow() 的问题
- c# - 如何使用多线程c#执行控制台应用程序
- ios - 如何弱引用作为参数传递的函数
- python - 控制 df.at 或 df.loc 的行为