2

我想知道如何将行索引用作条件,就像在下面的代码中使用列索引一样:

<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>

在此示例中,DataGrid 的整个第一列是制表符,但我只需要 DataGrid 的第一个单元格是制表符。我该怎么做?

4

1 回答 1

0

没有可以绑定的属性返回行的索引,但是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>

但是,您不能直接绑定到方法,因此您必须使用转换器。

于 2017-03-03T22:20:57.343 回答