[英]How to use row index as a condition in a multitrigger for a DataGridCell style in WPF XAML?
我想知道如何將行索引用作條件,就像在下面的代碼中使用列索引一樣:
<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的整個第一列都是tabstop,但我只需要將DataGrid的第一單元格設置為tabstop。 我該怎么做?
沒有可以綁定到的屬性可以返回行的索引,但是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>
但是,您不能直接綁定到方法,因此必須使用轉換器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.