简体   繁体   English

如何在WPF XAML中的DataGridCell样式的多触发条件中使用行索引作为条件?

[英]How to use row index as a condition in a multitrigger for a DataGridCell style in WPF XAML?

I want to know how to use the row index as a condition in the same way the column index is used in the code below: 我想知道如何将行索引用作条件,就像在下面的代码中使用列索引一样:

<Style x:Key="DefaultDataGridCell" TargetType="{x:Type DataGridCell}">
<Setter Property="IsTabStop" Value="False" />
<Style.Triggers>
    <MultiDataTrigger>
        <MultiDataTrigger.Conditions>
            <Condition Binding="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=Column.DisplayIndex}" Value="0" />
        </MultiDataTrigger.Conditions>
        <Setter Property="IsTabStop" Value="True" />
    </MultiDataTrigger>
</Style.Triggers>

In this example the entire first column of the DataGrid is tabstop but I only need the first cell of the DataGrid to be tabstop. 在此示例中,DataGrid的整个第一列都是tabstop,但我只需要将DataGrid的第一单元格设置为tabstop。 How can I do it? 我该怎么做?

There is no property that you can bind to that returns the index of the row but the DataGridRow class has a GetIndex() method that you could call in a converter class: 没有可以绑定到的属性可以返回行的索引,但是DataGridRow类具有可在转换器类中调用的GetIndex()方法:

namespace WpfApplication1
{

    public class MyConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return (value as DataGridRow).GetIndex();
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

<Style x:Key="DefaultDataGridCell" TargetType="{x:Type DataGridCell}"
               xmlns:local="clr-namespace:WpfApplication1">
    <Style.Resources>
        <local:MyConverter x:Key="conv" />
    </Style.Resources>
    <Setter Property="IsTabStop" Value="False" />
    <Style.Triggers>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=Column.DisplayIndex}" Value="0" />
                <Condition Binding="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow}, Converter={StaticResource conv}}" Value="0" />
            </MultiDataTrigger.Conditions>
            <Setter Property="IsTabStop" Value="True" />
        </MultiDataTrigger>
    </Style.Triggers>
</Style>

You cannot bind directly to a method though so you will have to use a converter. 但是,您不能直接绑定到方法,因此必须使用转换器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM