如何根据 DataGridColumn 中的信息设置 DataGridHeader 的样式?

发布于 2024-10-20 18:14:08 字数 1883 浏览 1 评论 0原文

我有一个 DataGrid,在该网格中,某些列被标记为只读:

<DataGrid AutoGenerateColumns="False">
    <DataGrid.Columns>
        <!-- this column is read only -->
        <DataGridTextColumn Header="Column A" Binding="{Binding Path=PropertyA}" IsReadOnly="True" />

        <!-- this column is EDITABLE -->
        <DataGridTextColumn Header="Name" Binding="{Binding Path=Name,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" IsReadOnly="False" />

        <!-- this column is read only -->
        <DataGridTextColumn Header="Column C" Binding="{Binding Path=PropertyC}" IsReadOnly="True" />
    </DataGrid.Columns>

我希望“名称”列在视觉上可以通过标题来区分,因为它是可编辑的,而其他两列则不可编辑。不过,我似乎无法访问 DataGridColumn 的 IsReadOnly 属性。

我实际上正在尝试做类似的事情:

<DataGrid.ColumnHeaderStyle>
     <Style TargetType="DataGridColumnHeader" >
         <Style.Triggers>
             <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=DataGridColumn}, Path=IsReadOnly}" Value="false">
                 <Setter Property="Background" Value="Azure" />
              </DataTrigger>
          </Style.Triggers>
      </Style>                                  
</DataGrid.ColumnHeaderStyle>

从这个问题: 在 WPF DataGrid 中绑定 DataGridColumn 的 Visible 属性,显示DataGridColumn 不是框架元素,因此我无法使用 RelativeSource AncestorType=DataGridColumn 找到它。该海报说他们使用静态资源来找到它,但没有解释什么/如何(以及几个答案,其中有关于海报如何解决它的问题)

这个问题: 如何从 DataGridColumn 获取 DataGridColumnHeader?,看起来我可以从代码获取它,但我会我真的很喜欢它只是 xaml 和通用的,适用于任何数据网格。

我忽略了一些简单的事情吗?

I have a DataGrid, and in that grid, some of the columns are marked Read only:

<DataGrid AutoGenerateColumns="False">
    <DataGrid.Columns>
        <!-- this column is read only -->
        <DataGridTextColumn Header="Column A" Binding="{Binding Path=PropertyA}" IsReadOnly="True" />

        <!-- this column is EDITABLE -->
        <DataGridTextColumn Header="Name" Binding="{Binding Path=Name,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" IsReadOnly="False" />

        <!-- this column is read only -->
        <DataGridTextColumn Header="Column C" Binding="{Binding Path=PropertyC}" IsReadOnly="True" />
    </DataGrid.Columns>

I want that "Name" column to be visually distinguishable by the header that it is editable, when the other two columns are not. I can't seeem to get to the IsReadOnly property of the DataGridColumn, though.

I'm effectively trying to do something like:

<DataGrid.ColumnHeaderStyle>
     <Style TargetType="DataGridColumnHeader" >
         <Style.Triggers>
             <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=DataGridColumn}, Path=IsReadOnly}" Value="false">
                 <Setter Property="Background" Value="Azure" />
              </DataTrigger>
          </Style.Triggers>
      </Style>                                  
</DataGrid.ColumnHeaderStyle>

From this question:
Binding Visible property of a DataGridColumn in WPF DataGrid, it appears that DataGridColumn isn't a framework element, so i can't find it using RelativeSource AncestorType=DataGridColumn. That poster says they used a static resource to find it, but doesn't explain what/how (and several answers there are questions of how the poster solved it)

This question: How to get DataGridColumnHeader from DataGridColumn?, looks like i could get to it from code, but i'd really like this to just be xaml and generic to apply to any data grid.

Is there something simple i'm overlooking?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

要走就滚别墨迹 2024-10-27 18:14:08

回答我自己的问题,以防其他人遇到这个问题...

事实证明,由于问题中提到的一些事情,您无法通过 XAML 在这里做很多事情。我可以从 XAML 做的最简单的事情就是将可编辑列的标题设为粗体,以将它们与其余列区分开来。

<DataGridTextColumn Header="Editable Column" Binding="{Binding Path=EditableProperty,Mode=TwoWay}" IsReadOnly="False" Width="150">
    <DataGridTextColumn.HeaderStyle>
        <Style TargetType="DataGridColumnHeader">
            <Setter Property="FontWeight" Value="Bold"/>
            <Setter Property="ToolTip" Value="You can modify the values of this column."/>
        </Style>
    </DataGridTextColumn.HeaderStyle>
</DataGridTextColumn>

可以做更复杂的事情,例如:

<Style.Triggers>
    <Trigger Property="IsMouseOver" Value="False">
        <Trigger.Setters>
            <Setter Property="Background">
                <Setter.Value>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="{DynamicResource ControlLightColor}" Offset="0" />
                        <GradientStop Color="LightSteelBlue" Offset="1" />
                    </LinearGradientBrush>
                </Setter.Value>
            </Setter>
        </Trigger.Setters>
    </Trigger>
</Style.Triggers>  

但是您最终会得到一些非常奇怪的行为,排序图标消失,或其他奇怪的事情。如果您想更改列标题并使其看起来一致,您几乎必须通过样式和模板重新设置整个数据网格及其所有标题的样式。

Answering my own question, in case anyone else runs into this...

It turns out there isn't a lot you can do here from XAML, because of some of the things mentioned in the question. The simplest thing i could do from XAML was make the headers of the editable columns bold to differentiate them from the rest of the columns.

<DataGridTextColumn Header="Editable Column" Binding="{Binding Path=EditableProperty,Mode=TwoWay}" IsReadOnly="False" Width="150">
    <DataGridTextColumn.HeaderStyle>
        <Style TargetType="DataGridColumnHeader">
            <Setter Property="FontWeight" Value="Bold"/>
            <Setter Property="ToolTip" Value="You can modify the values of this column."/>
        </Style>
    </DataGridTextColumn.HeaderStyle>
</DataGridTextColumn>

You can do more complicated things, like:

<Style.Triggers>
    <Trigger Property="IsMouseOver" Value="False">
        <Trigger.Setters>
            <Setter Property="Background">
                <Setter.Value>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="{DynamicResource ControlLightColor}" Offset="0" />
                        <GradientStop Color="LightSteelBlue" Offset="1" />
                    </LinearGradientBrush>
                </Setter.Value>
            </Setter>
        </Trigger.Setters>
    </Trigger>
</Style.Triggers>  

But you end up getting some really wierd behaviors, where the sort icon disappears, or other wierd things. If you want to change the column headers and make it look consistant, you pretty much have to restyle the whole datagrid and all of its headers through styles and templates.

爱给你人给你 2024-10-27 18:14:08

DataGridColumnHeader 具有属性 Column,该属性具有属性 IsReadOnly

<Label  Content="(read only)" DockPanel.Dock="Bottom">
<Label.Style>
    <Style TargetType="Label">
        <Setter Property="Visibility" Value="Collapsed"></Setter>
        <Style.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=DataGridColumnHeader},Path=Column.IsReadOnly}" Value="True">
                <Setter Property="Visibility" Value="Visible"></Setter>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Label.Style>

在您的示例中,您可以执行以下操作:

<DataGrid.ColumnHeaderStyle>
 <Style TargetType="DataGridColumnHeader" >
     <Style.Triggers>
         <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=DataGridColumnHeader}, Path=Column.IsReadOnly}" Value="False">
             <Setter Property="Background" Value="Azure" />
          </DataTrigger>
      </Style.Triggers>
  </Style>                                  

DataGridColumnHeader has property Column, which has the property IsReadOnly

<Label  Content="(read only)" DockPanel.Dock="Bottom">
<Label.Style>
    <Style TargetType="Label">
        <Setter Property="Visibility" Value="Collapsed"></Setter>
        <Style.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=DataGridColumnHeader},Path=Column.IsReadOnly}" Value="True">
                <Setter Property="Visibility" Value="Visible"></Setter>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Label.Style>

In your example you could do:

<DataGrid.ColumnHeaderStyle>
 <Style TargetType="DataGridColumnHeader" >
     <Style.Triggers>
         <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=DataGridColumnHeader}, Path=Column.IsReadOnly}" Value="False">
             <Setter Property="Background" Value="Azure" />
          </DataTrigger>
      </Style.Triggers>
  </Style>                                  

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文