wpf 相对源数据触发器绑定不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29129117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:01:17  来源:igfitidea点击:

RelativeSource data trigger binding not working

wpfstyleswpf-style

提问by Sloth Armstrong

I am trying to set the background color of a DataGridTextColumnto another color if it is read-only. I am doing so with the following code:

DataGridTextColumn如果a是只读的,我试图将它的背景颜色设置为另一种颜色。我正在使用以下代码执行此操作:

<DataGridTextColumn Header="Test" IsReadOnly="True">
    <DataGridTextColumn.ElementStyle>
        <Style TargetType="{x:Type TextBlock}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsReadOnly, RelativeSource={RelativeSource Mode=FindAncestor,  AncestorType={x:Type DataGridTextColumn}}}" Value="True">
                    <Setter Property="Background" Value="LightGreen"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGridTextColumn.ElementStyle>
</DataGridTextColumn>

I am having no luck, however removing the triggers results in the background always being light green. Is something wrong with the data trigger binding? I am relatively new to WPF but this is what I could find online. Ideally this would be in App.XAML so it would work across all columns such as this, so would there then be a way to translate this to a style? Thanks.

我运气不好,但是删除触发器会导致背景始终为浅绿色。数据触发器绑定有问题吗?我对 WPF 比较陌生,但这是我可以在网上找到的。理想情况下,这将在 App.XAML 中,因此它可以在所有列中工作,例如这样,那么有没有办法将其转换为样式?谢谢。

Edit---------

编辑 - - - - -

If I bind by ElementName it works:

如果我通过 ElementName 绑定它可以工作:

<DataTrigger Binding="{Binding IsReadOnly, ElementName=stupid}" Value="True">
        <Setter Property="Foreground" Value="Red" />
</DataTrigger>

However I would like this to be more generic if possible. Thanks again.

但是,如果可能的话,我希望这更通用。再次感谢。

采纳答案by Clark

Edit: Didn't check for a background property on DataGridTextColumn at first.

编辑:起初没有检查 DataGridTextColumn 上的背景属性。

This answered your original question -

这回答了您最初的问题-

<DataGridTextColumn Header="Test" IsReadOnly="True" Binding="{Binding name}" x:Name="MyColumn">
 <DataGridTextColumn.ElementStyle>
  <Style TargetType="{x:Type TextBlock}">
   <Style.Triggers>
    <DataTrigger Binding="{Binding IsReadOnly, ElementName=MyColumn}" Value="True">
     <Setter Property="Background" Value="Orange" />
    </DataTrigger>
   </Style.Triggers>
  </Style>
 </DataGridTextColumn.ElementStyle>
</DataGridTextColumn>  

To answer your second question, the DataTrigger binidng you are looking for is:

要回答您的第二个问题,您正在寻找的 DataTrigger binidng 是:

<DataTrigger Binding="{Binding IsReadOnly, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridCell}}}" Value="True">

In Summary, look for the parent DataGridCell instead of DataGridTextColumn. The reason for this is the TextBlock you are trying to style is not actually a child of DataGridTextColumn, but a child of the DataGridTextColumn's peer.

总之,查找父 DataGridCell 而不是 DataGridTextColumn。这样做的原因是您尝试设置样式的 TextBlock 实际上不是 DataGridTextColumn 的子项,而是 DataGridTextColumn 对等项的子项。