【发布时间】:2014-09-28 18:15:34
【问题描述】:
我有一个带有 AlternatingRowBackground(白色和蓝色)的 DataGrid。但是,某些选项可以使某些行只读(例如,行的后半部分),在这种情况下,背景将更改为浅灰色。 DataGrid 是动态填充的,因此所有这些背景更改都是在代码隐藏中完成的。
如果 DataGrid 的前半部分行是可编辑的,而后半部分是只读的,例如,代码是
if (Symmetric.IsChecked == true)
{
int n = (nPoints % 2 == 0 ? nPoints / 2 : (nPoints + 1) / 2);
for (int i = 1; i < n; i++)
{
//resultSections is the DataContext of the (DataGrid)SectionsGrid
resultSections[i].IsReadOnly = false;
var r = SectionsGrid.GetRow(i);
if (r == null)
continue;
r.Background = (i % 2 == 0 ? Brushes.White : Brushes.AliceBlue);
}
for (int i = n; i < nPoints; i++)
{
resultSections[i].X = ProjectProperties.I.BeamLength - resultSections[nPoints - i - 1].X;
resultSections[i].IsReadOnly = true;
var r = SectionsGrid.GetRow(i);
if (r == null)
continue;
r.Background = Brushes.LightGray;
}
}
只要所有行都在视图中,此代码就可以正常工作。如果存在只能通过滚动查看的行,则 SectionsGrid.GetRow(i) 返回 null 并且背景不会改变。有没有办法在不绘制行的情况下设置行的背景?
我不知道如何使用 DataTriggers 在 .xaml 中执行此操作,我知道它通常用于定义不断变化的背景。问题是背景取决于是否选中了两个 CheckBoxes 中的任何一个(只能选中一个),而行为因 CheckBox 而异。此外,如果其中一个 CheckBoxes 被选中然后取消选中,则背景需要恢复为 AlternatingRowBackground,这又取决于行号。
编辑:
我意识到将 DataTrigger 设置为行的 DataContext 的 .IsReadOnly 属性可能有效,因此我创建了以下内容:
<Style x:Key="ReadOnlyCheck" TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsReadOnly}" Value="True">
<Setter Property="Tag" Value="ReadOnly" />
<Setter Property="Background" Value="LightGray"/>
</DataTrigger>
</Style.Triggers>
这适用于设置.IsReadOnly 将背景变为LightGray,但它仍然在超出“滚动范围”的行上失败。一旦我滚动,它们并没有变灰。
【问题讨论】: