【发布时间】:2019-03-20 21:33:03
【问题描述】:
例如,考虑 WPF DataGrid 和 DataGridTemplateColumn(s) 并允许用户启动添加 CanUserAddRows="True"
<DataGrid AutoGenerateColumns="False" CanUserAddRows="True" ItemsSource="{Binding Options}">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<RadioButton IsChecked="{Binding IsChecked}" GroupName="OptionsRad"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Option" Binding="{Binding OptionName}"/>
</DataGrid.Columns>
</DataGrid>
由于托管DataGridTemplateColumn,RadioButton 可以在不进入 CellEditingMode 的情况下进行交互,这是预期和期望的。
然而,有一个问题是,在最后一个“添加新选项”,行类型 RadioButton 可以与 甚至在添加新选项之前进行交互(即将添加名称已输入)。可能选择不存在的选项并将焦点转移到其他地方。
如何在将新行添加到绑定集合之前禁用与模板列的交互?
【问题讨论】:
-
有一个属性DataGridRow.IsNewItem,您可以将其与相对源绑定(like this)。表示您可以在单元格模板 (like this) 中使用 datatrigger 来隐藏
RadioButton。 -
@Sinatr 几乎 - 但是它永远保持禁用状态。无法选择任何新选项。似乎
DataGridRow.IsNewItem在所有新项目上都是正确的,而不仅仅是添加新行。 (与Binding = "{Binding IsNewItem, RelativeSource = {RelativeSource Mode = FindAncestor, AncestorType = DataGridRow}}")
标签: c# wpf wpfdatagrid