如何将背景颜色绑定到另一个 IsSelected 背景

发布于 2024-12-29 01:30:59 字数 667 浏览 8 评论 0原文

我想将 MyDataGrid 的选定背景颜色绑定到另一个 IsSelected 背景颜色,以便它们共享相同的颜色。我想可以像下面这样完成。我该怎么做呢?

                <DataGrid.Resources>
                    <Style TargetType="{x:Type DataGridCell}">
                        <Style.Triggers>
                            <Trigger Property="DataGridCell.IsSelected" Value="True">
                                <Setter Property="Background" Value="{Binding ElementName=OtherDataGrid, Path=??Background??" />
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </DataGrid.Resources>

I want to bind the selected Background color of MyDataGrid to another IsSelected Background color so they share the same color. I'm thinking it can be done something like below. How can I do it?

                <DataGrid.Resources>
                    <Style TargetType="{x:Type DataGridCell}">
                        <Style.Triggers>
                            <Trigger Property="DataGridCell.IsSelected" Value="True">
                                <Setter Property="Background" Value="{Binding ElementName=OtherDataGrid, Path=??Background??" />
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </DataGrid.Resources>

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

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

发布评论

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

评论(3

微凉徒眸意 2025-01-05 01:30:59

共享背景的最佳方法是使用StaticResource

The best way to share the background is to use a StaticResource.

心清如水 2025-01-05 01:30:59

您可以在资源中创建画笔并在两个数据网格中引用它。

喜欢:

<Style.Triggers>
    <Trigger Property="DataGridCell.IsSelected" Value="True">
         <Setter Property="Background" Value="{StaticResource selectedCellBackground}" />
    </Trigger>
</Style.Triggers>

You can create a brush in resources and refer that in both the data grids.

Like:

<Style.Triggers>
    <Trigger Property="DataGridCell.IsSelected" Value="True">
         <Setter Property="Background" Value="{StaticResource selectedCellBackground}" />
    </Trigger>
</Style.Triggers>
謸气贵蔟 2025-01-05 01:30:59

另一种方法是在视图模型中声明一个通知属性并将两种颜色绑定到它。

Another way is to declare a notify property in the view model and bind both colors to it.

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