【问题标题】:Change color of DataGridHyperlinkColumn when row is unfocused行未聚焦时更改 DataGridHyperlinkColumn 的颜色
【发布时间】:2017-04-25 10:48:17
【问题描述】:

我正在尝试在选择 DataGridRow 时更改 DataGridHyperlinkColumn 的颜色,但随后失去焦点。它似乎不尊重以下内容:

               <Style TargetType="Hyperlink">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}, Path=IsSelected}" Value="True">
                            <Setter Property="Foreground" Value="White"/>
                        </DataTrigger>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Foreground" Value="Red"/>
                        </Trigger>
                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}, Path=IsFocused}" Value="False">
                            <Setter Property="Foreground" Value="Blue"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>

【问题讨论】:

    标签: wpf datagrid


    【解决方案1】:

    问题是 DataGridRow 总是不聚焦,聚焦的是单元格:

    <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridCell}}, Path=IsFocused}" Value="False">
    

    像这样绑定到父 DataGridCell 仅在您有单列时才有效。如果您有几列单元格旁边的单元格而不是包含超链接的单元格,那么您不能简单地使用 RelativeSource 绑定来绑定到父单元格并检查其 IsFocused 属性。

    然后您将不得不做一些更复杂的事情,例如处理 DataGrid 的 GotFocus/LostFocus 路由事件并将其 Tag 属性设置为 true/false:

        private void dg_GotFocus(object sender, RoutedEventArgs e)
        {
            dg.Tag = true;
        }
    
        private void dg_LostFocus(object sender, RoutedEventArgs e)
        {
            dg.Tag = false;
        }
    

    然后您可以将 DataTrigger 绑定到 DataGrid 的 Tag 属性,而不是绑定到父 DataGridCell 的 IsFocused 属性:

    <DataGrid x:Name="dg" GotFocus="dg_GotFocus" LostFocus="dg_LostFocus">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Text}" />
                <DataGridHyperlinkColumn Binding="{Binding Link}">
                    <DataGridHyperlinkColumn.CellStyle>
                        <Style TargetType="DataGridCell">
                            <Style.Resources>
                                <local:Converter x:Key="converter" />
                                <Style TargetType="Hyperlink">
                                    <Setter Property="Foreground" Value="Chocolate"></Setter>
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}, Path=IsSelected}" Value="True">
                                            <Setter Property="Foreground" Value="White"/>
                                        </DataTrigger>
                                        <Trigger Property="IsMouseOver" Value="True">
                                            <Setter Property="Foreground" Value="Red"/>
                                        </Trigger>
                                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=Tag}" 
                                                     Value="False">
                                            <Setter Property="Foreground" Value="Blue"/>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </Style.Resources>
                        </Style>
                    </DataGridHyperlinkColumn.CellStyle>
                </DataGridHyperlinkColumn>
            </DataGrid.Columns>
        </DataGrid>
    

    感谢 mm8,按预期工作。不过,这确实造成了一个意想不到的问题。现在不尊重单元格填充。我会在哪里应用类似

    在列的ElementStyle中:

    <DataGridHyperlinkColumn Binding="{Binding Link}">
                    <DataGridHyperlinkColumn.ElementStyle>
                        <Style TargetType="TextBlock">
                            <Setter Property="Padding" Value="10,5,10,5" />
                        </Style>
                    </DataGridHyperlinkColumn.ElementStyle>
                    <DataGridHyperlinkColumn.CellStyle>
                    ...
      </DataGridHyperlinkColumn>
    

    【讨论】:

    • 感谢 mm8,按预期工作。不过,这确实造成了一个意想不到的问题。现在不尊重单元格填充。我会在哪里申请&lt;Setter Property="Padding" Value="10,5,10,5" /&gt;
    • 在列的ElementStyle中。我编辑了我的答案。如果对您有帮助,请记得投票 :)
    猜你喜欢
    • 2016-08-09
    • 2014-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-04
    • 1970-01-01
    • 2012-03-02
    • 2019-09-28
    相关资源
    最近更新 更多