如何检查一行是否有奇数? [英] How to check if a row has odd number?
问题描述
我正在尝试使用XAML为奇数行设置不同的颜色.
I'm trying to set different color for odd rows using XAML.
有问题的数据网格具有3种不同类型的数据,我想用不同的颜色着色,并且简单地更改AlternatingRowBackground不会实现.
The datagrid in question has 3 different types of data, which I want to color differently, and simply changing AlternatingRowBackground won't do.
我打算使用类似的东西
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Type}" Value="0"/>
<Condition Binding="{Binding IsSelected, RelativeSource={RelativeSource Self}}" Value="False"/>
<Condition Binding="{Binding IsOddRow, RelativeSource={RelativeSource Self}}" Value="False"/>
</MultiDataTrigger.Conditions>
<Setter Property="Background" Value="#FFDFE6ED"/>
</MultiDataTrigger>
似乎没有像IsOddRow
这样的属性.我应该检查哪个属性?
There doesn't seem to be such a property as IsOddRow
. What property should I check instead?
推荐答案
您可以在DataGrid
上设置AlternationCount
,然后绑定到祖先DataGridRows
附加属性ItemsControl.AlternationIndex
.如果值为"1",则您的行号为奇数.
You can set AlternationCount
on the DataGrid
and then bind to the ancestor DataGridRows
attached property ItemsControl.AlternationIndex
. If the value is "1" you have an odd row number.
<DataGrid ItemsSource="{Binding ...}"
AlternationCount="2">
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Type}" Value="0"/>
<Condition Binding="{Binding RelativeSource={RelativeSource Self},
Path=IsSelected}"
Value="False"/>
<Condition Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}},
Path=(ItemsControl.AlternationIndex)}"
Value="1"/>
</MultiDataTrigger.Conditions>
<Setter Property="Background" Value="#FFDFE6ED"/>
</MultiDataTrigger>
<!-- ... -->
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
<!-- ... -->
</DataGrid>
请注意,绑定到附加属性时,必须在附加属性周围加上括号. Path=(ItemsControl.AlternationIndex)
将起作用,但Path=ItemsControl.AlternationIndex
将不起作用.
Note that when binding to an attached property, you must put parentheses around the attached property. Path=(ItemsControl.AlternationIndex)
will work but Path=ItemsControl.AlternationIndex
won't.
更新
您也可以通过附加行为创建属性IsOddRow
.在行为中,您预订LoadingRow
.在事件处理程序中,您将获取已加载行的索引,并检查其是否为奇数.然后将结果存储在名为IsOddRow
的附加属性中,您可以将其绑定到该属性.
Update
You could also create the property IsOddRow
through an attached behavior. In the behavior you subscribe to LoadingRow
. In the event handler you get the index for the loaded row and check if it is odd or not. The result is then stored in an attached property called IsOddRow
which you can bind to.
要开始该行为,请将behaviors:DataGridBehavior.ObserveOddRow="True"
添加到DataGrid
To start the behavior add behaviors:DataGridBehavior.ObserveOddRow="True"
to the DataGrid
<DataGrid ItemsSource="{Binding ...}"
behaviors:DataGridBehavior.ObserveOddRow="True">
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Type}" Value="0"/>
<Condition Binding="{Binding Path=IsSelected,
RelativeSource={RelativeSource Self}}" Value="False"/>
<Condition Binding="{Binding Path=(behaviors:DataGridBehavior.IsOddRow),
RelativeSource={RelativeSource Self}}" Value="False"/>
</MultiDataTrigger.Conditions>
<Setter Property="Background" Value="#FFDFE6ED"/>
</MultiDataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
</DataGrid>
DataGridBehavior
public class DataGridBehavior
{
#region ObserveOddRow
public static readonly DependencyProperty ObserveOddRowProperty =
DependencyProperty.RegisterAttached("ObserveOddRow",
typeof(bool),
typeof(DataGridBehavior),
new UIPropertyMetadata(false, OnObserveOddRowChanged));
[AttachedPropertyBrowsableForType(typeof(DataGrid))]
public static bool GetObserveOddRow(DataGrid dataGrid)
{
return (bool)dataGrid.GetValue(ObserveOddRowProperty);
}
public static void SetObserveOddRow(DataGrid dataGrid, bool value)
{
dataGrid.SetValue(ObserveOddRowProperty, value);
}
private static void OnObserveOddRowChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
{
DataGrid dataGrid = target as DataGrid;
dataGrid.LoadingRow += (object sender, DataGridRowEventArgs e2) =>
{
DataGridRow dataGridRow = e2.Row;
bool isOddRow = dataGridRow.GetIndex() % 2 != 0;
SetIsOddRow(dataGridRow, isOddRow);
};
}
#endregion // ObserveOddRow
#region IsOddRow
public static DependencyProperty IsOddRowProperty =
DependencyProperty.RegisterAttached("IsOddRow",
typeof(bool),
typeof(DataGridBehavior),
new PropertyMetadata(false));
[AttachedPropertyBrowsableForType(typeof(DataGridRow))]
public static bool GetIsOddRow(DataGridRow dataGridCell)
{
return (bool)dataGridCell.GetValue(IsOddRowProperty);
}
public static void SetIsOddRow(DataGridRow dataGridCell, bool value)
{
dataGridCell.SetValue(IsOddRowProperty, value);
}
#endregion // IsOddRow
}
这篇关于如何检查一行是否有奇数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!