首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WPFdatagrid -如何在分组时使用(赋予)不同的颜色?

WPFdatagrid -如何在分组时使用(赋予)不同的颜色?
EN

Stack Overflow用户
提问于 2011-09-19 18:35:50
回答 1查看 1.4K关注 0票数 1

我正在使用codeplex的WPF数据网格。

我有一个具有分组功能的wpf网格。我想要不同颜色的分组区域。截图如下:

在分组时可以分配不同的颜色吗?如果是,如何在WPF数据网格中实现这一点?

EN

回答 1

Stack Overflow用户

发布于 2011-09-19 21:01:24

希望这能帮到你。

代码语言:javascript
复制
<GroupStyle>
  <GroupStyle.HeaderTemplate>
    <DataTemplate>
       <StackPanel>
         <TextBlock Text="{Binding Path=Name}" />
        </StackPanel>
     </DataTemplate>
  </GroupStyle.HeaderTemplate>
  <GroupStyle.ContainerStyle>
     <Style TargetType="{x:Type GroupItem}">
        <Setter Property="Template">
           <Setter.Value>
              <ControlTemplate TargetType="{x:Type GroupItem}">
                 <Expander>
                    <Expander.Header>
                       <StackPanel Orientation="Horizontal">
                          <TextBlock Text="{Binding Path=Name}" />
                          <TextBlock Text=" ("/>
                          <TextBlock Text="{Binding Path=ItemCount}"/>
                          <TextBlock Text=" "/>
                          <TextBlock Text="Items"/>
                          <TextBlock Text=")"/>
                       </StackPanel>
                     </Expander.Header>
                     <ItemsPresenter>
                        <ItemsPresenter.Resources>
                           <Style TargetType="{x:Type toolkit:DataGridRow}">
                              <Style.Triggers>
                                 <DataTrigger
                                    Binding="{Binding RelativeSource=
                                      {RelativeSource AncestorType={x:Type 
                                        GroupItem}}, Path=DataContext.Name}"
                                    Value="1">
                                   <Setter Property="Background"
                                           Value="LightGreen"/>
                                 </DataTrigger>
                                 <DataTrigger
                                    Binding="{Binding RelativeSource=
                                      {RelativeSource AncestorType={x:Type 
                                        GroupItem}}, Path=DataContext.Name}"
                                    Value="2">
                                   <Setter Property="Background"
                                           Value="LightPink"/>
                                 </DataTrigger>
                               </Style.Triggers>
                             </Style>
                           </ItemsPresenter.Resources>
                         </ItemsPresenter>
                       </Expander>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
   </GroupStyle.ContainerStyle>
</GroupStyle>

上面的数据触发器检查我们在哪个值上创建了组,并相应地分配数据网格行背景色。

因此,第一组表示文本"1“(LightGreen)下的所有值,下一组分组在值2 (LightPink)下。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7469583

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档