【发布时间】:2015-05-07 09:54:10
【问题描述】:
我正在尝试创建一个 DataGrid,用户可以在其中编辑数据并在之后应用 oder 取消更改。
另外一个DataGridCell 有一个TemplateSelector 来帮助用户输入有效数据。 (DateTime, Boolean,...)
我的模型有一些属性。对于我的问题,有两个相关的:
Model.Type 和 Model.Value
在 CodeBehind 中,我的属性具有以下类型:
enum Type;
string Value;
当用户编辑 Value 时,TemplateSelector 应该根据 Type 获得正确的 DataTemplate
我的 XAML 如下所示:
<DataGrid x:Name="datagrid"
ItemsSource="{Binding Variables}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
AutoGenerateColumns="False"
IsSynchronizedWithCurrentItem="True">
<DataGrid.Columns>
<DataGridComboBoxColumn Header="{StaticResource VariablesType}"
ItemsSource="{Binding Source={StaticResource VariableTypes}, Mode=OneWay}"
SelectedItemBinding="{Binding Type}"
Width="80"/>
<DataGridTemplateColumn Header="{StaticResource VariablesValue}" Width="2*">
<DataGridTemplateColumn.CellEditingTemplateSelector>
<TemplateSelector:TemplateSelector_Variables>
<!--Definitin of Templates,..-->
</TemplateSelector:TemplateSelector_Variables>
</DataGridTemplateColumn.CellEditingTemplateSelector>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Value}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
让我们进入 TemplateSelector:
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
if (item == null)
return base.SelectTemplate(item, container);
RaVariableType VariablenType = ((RaVariable)item).Type;
//SelectingLogic
//Here I would need the CURRENT type but it gives me the type of my Model
}
我已经有了解决办法: 如果我使用:
SelectedItemBinding="{Binding Type, UpdateSourceTrigger="PropertyChanged"}"
它可以工作,但我想处理用户的 OK 或 Cancel,并且此代码更改了我的模型。
另一个问题: 你如何处理这样的决定。 (如何更新模型-命令的代码)
谢谢!
【问题讨论】:
标签: c# wpf xaml binding datagrid