首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何覆盖DataGridCell样式

如何覆盖DataGridCell样式
EN

Stack Overflow用户
提问于 2022-06-21 11:09:27
回答 1查看 80关注 0票数 0

我有一个数据,想要样式它的细胞。我有4个列,2个是文本列,2个是用于编辑和删除按钮的自定义列,如下所示。我的目的是改变它们的风格。当鼠标在文本列上时,单元格背景将不同于编辑和删除列。我为DataGrid提供了一个通用样式,并在其中包含了DataGridCell的新样式。如何为编辑和删除按钮定义新样式并在xaml文件中设置此新样式?

风格

代码语言:javascript
复制
<Style TargetType="{x:Type DataGrid}">
    <Style.Resources>
      <Style TargetType="{x:Type DataGridCell}">
          <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridCell}">
              <ControlTemplate.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                  <Setter Property="Background" Value="{StaticResource ControlBackgroundLine}" />
                </Trigger>
                <Trigger Property="IsSelected" Value="True">
                  <Setter Property="Background" Value="{StaticResource BackgroundSelected}" />
                </Trigger>
              </ControlTemplate.Triggers>
            </ControlTemplate>
          </Setter.Value>
        </Setter>
      </Style>
    </Style.Resources>
  </Style>

XAML

代码语言:javascript
复制
<DataGrid Grid.Row="3" ItemsSource="{Binding deckList}" AutoGenerateColumns="False" 
            SelectionMode="Single" SelectionUnit="FullRow" Margin="10,10,0,0" >
            <DataGrid.Columns>
                <DataGridTextColumn Header="Name" Binding="{Binding Path=Name}" ></DataGridTextColumn>
                <DataGridTextColumn Header="SurName" Binding="{Binding Path=SurName}"></DataGridTextColumn>
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button Width="32">
                                <Image Source="/KillCard;component/Resources/Images/delete.png" Width="16"></Image>
                            </Button>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button Width="32">
                                <Image Source="/KillCard;component/Resources/Images/edit.png" Width="16"></Image>
                            </Button>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
   </DataGrid>
EN

回答 1

Stack Overflow用户

发布于 2022-06-21 12:01:03

可以将DataGrid.CellStyle属性设置为全局配置单元格(在当前DataGrid的范围内):

代码语言:javascript
复制
<DataGrid>
  <DataGrid.CellStyle>
    <Style TargetType="{x:Type DataGridCell}">
      <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
          <Setter Property="Background" Value="{StaticResource ControlBackgroundLine}" />
        </Trigger>
      </Style.Triggers>
    </Style>
  </DataGrid.CellStyle>
</DataGrid>

或者,若要配置单个单元格,请设置DataGridColumn.CellStyle属性:

代码语言:javascript
复制
<DataGrid>
  <DataGrid.Columns>
    <DataGridTextColumn>
      <DataGridTextColumn.CellStyle>
        <Style TargetType="{x:Type DataGridCell}">
          <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
              <Setter Property="Background" Value="{StaticResource ControlBackgroundLine}" />
            </Trigger>
          </Style.Triggers>
        </Style>
      </DataGridTextColumn.CellStyle>
    </DataGridTextColumn>
  </DataGrid.Columns>
</DataGrid>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72699720

复制
相关文章

相似问题

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