突出显示DataGrid中的一个项目 [英] Highlight one item in DataGrid

查看:112
本文介绍了突出显示DataGrid中的一个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在DataGrid中显示 Cars ,并且想突出显示一辆专用车, CurrentlySelectedCar

I'm displaying Cars in a DataGrid and would like to highlight one special car, the CurrentlySelectedCar.

当用户双击车时,此车将保存为MainViewModel中的 CurrentlySelectedCar 。现在,每当用户回到DataGrid,我想突出显示这个car =行,例如通过使用红色背景。

When the user double clicks on a car, this car is saved as CurrentlySelectedCar in my MainViewModel. Now, whenever the user comes back to the DataGrid, I would like to highlight this car = row, e.g. by using a red background.

我已经发现如何根据某些值突出显示DataGrid中的行,但在我的例子中,我所有的都是CurrentlySelectedCar。

I have found out how to highlight rows in a DataGrid based on certain values, but in my case, all I have is the CurrentlySelectedCar.

我的第一个尝试:

            <Style TargetType="DataGridRow">
                <Style.Triggers>
                    <!-- not working-->
                    <DataTrigger Binding="{Binding CurrentlySelectedCar}" >
                        <Setter Property="Background" Value="Red"></Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>

我的第二个尝试:

            <Style TargetType="DataGridRow">
                <Style.Triggers>
                    <!-- not working either, "Binding can only be set on DependencyProperty of DependecyObject"-->
                    <DataTrigger Binding="{Binding Guid}" Value="{Binding CurrentlySelectedCar.Guid}" >
                        <Setter Property="Background" Value="Red"></Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>

如何使用此信息突出显示该行?

推荐答案

我认为你必须做这样的回答中描述的类似的东西:在DataTrigger条件中使用绑定

I think that you have to do something like this described in this answer: Using bindings in DataTrigger condition

<Style TargetType="DataGridRow">
  <Style.Triggers>
    <DataTrigger Value="True">
      <DataTrigger.Binding>
        <MultiBinding Converter="{StaticResource someMultiConverter}">
          <Binding Path="Guid"></Binding>
          <Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type Datagrid}}" Path="CurrentlySelectedCar.Guid"></Binding>
        </MultiBinding>
      </DataTrigger.Binding>
      <Setter Property="Background" Value="Red"></Setter>
    </DataTrigger>
  </Style.Triggers>
</Style>

如果两个Guid相等,则必须编写一个返回true的多重转换器。

You have to write a multiconverter that return true if the two Guid are equals.

这篇关于突出显示DataGrid中的一个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆