【问题标题】:How to color Expander in a DataGrid based on a DataTrigger?如何在基于 DataTrigger 的 DataGrid 中为 Expander 着色?
【发布时间】:2021-04-18 04:38:19
【问题描述】:

我在基于DataTriggerDataGrid 中的Expander 着色时遇到问题。我尝试了很多东西,也尝试了很多线程,但没有运气。

我希望根据 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:

【问题讨论】:

    标签: c# wpf binding datagrid datatrigger


    【解决方案1】:

    当您对集合视图进行分组时,组的数据上下文是CollectionViewGroup。它公开了几个属性,例如ItemCountName,它们是您为当前组分组的属性的。因此,如果要将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属性,一个组可以包含多个项目,所以DataTrigger中应该考虑具体哪个项目的check属性,第一个,一个布尔值和在所有项目中,还有什么?

    在这里,我检查组中是否有恰好一个,并使用其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.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-01
      • 1970-01-01
      • 2014-01-28
      • 1970-01-01
      • 2011-07-01
      • 2019-03-19
      • 2013-07-07
      相关资源
      最近更新 更多