我相信我所要做的就是“简单”,所以我可能只是错过了一些明显的东西 .
在DataGrid中,我试图绑定一个CheckBox,以便在选中它时,其行的Background颜色将会改变 . 每行都有一个CheckBox . 我基本上实现了我自己的select-multiple-rows功能(这是产品要求,不要问),而且我还有其他所有工作,但这是所选行的可视化指示 .
我读过this question但我缺少答案的地方是"BooleanPropertyOnObjectBoundToRow" . 我也看了this question并尝试弄乱了一个RelativeSource,但没有运气 .
我在我的代码隐藏中创建了我的网格,但这是我用于行的当前样式(它定义了我的DataTrigger):
<Style x:Key="MyRowStyle" TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding IsChecked}" Value="True">
<Setter Property="Background" Value="Blue"/>
</DataTrigger>
</Style.Triggers>
</Style>
现在在我的代码隐藏中,我创建了我的DataGridTemplateColumn并使用Factory来创建我的复选框,这是我的Binding相关代码:
Binding checkBinding = new Binding("IsChecked");
checkBinding.Mode = BindingMode.OneWayToSource;
RelativeSource relativeSource = new RelativeSource();
relativeSource.AncestorType = typeof(DataGridRow);
relativeSource.Mode = RelativeSourceMode.FindAncestor;
checkBinding.RelativeSource = relativeSource;
factory.SetBinding(CheckBox.IsCheckedProperty, checkBinding);
可能感兴趣的是我将DataGrid的ItemsSource设置为DataTable,但我的CheckBox列在DataTable中没有相应的列 . 我只是单独添加模板列,也许缺少底层存储会影响这个?
在任何情况下,如果您需要更多信息,请告诉我 . 谢谢!
1 回答
这是一个使用C#类而不是DataSet的示例 .
XAML
C#