如何基于 DataTrigger 在 DataGrid 中为 Expander 着色?

问题描述

我在基于 ExpanderDataGrid 中的 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,5" />
               <Setter Property="Template">
                  <Setter.Value>
                     <ControlTemplate TargetType="{x:Type GroupItem}">
                        <Expander IsExpanded="False"
                                  BorderBrush="#FF002255"
                                  Foreground="Black"
                                  BorderThickness="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" />
                                 </StackPanel>
                              </StackPanel>
                           </Expander.Header>
                           <Expander.Content>
                              <ItemsPresenter />
                           </Expander.Content>
                        </Expander>
                     </ControlTemplate>
                  </Setter.Value>
               </Setter>
            </Style>
         </GroupStyle.ContainerStyle>
      </GroupStyle>
   </DataGrid.GroupStyle>
</DataGrid>

DataGrid

Screenshot of the DataGrid.

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)