【发布时间】:2014-09-05 15:48:55
【问题描述】:
我在 DataGrid 中有两个 DataGridComboBoxColumns(技术上是 DataGridTemplateColumns)。我正在使用 MVVM。
第一列的 ItemsSource 绑定到一个静态资源——那里没有问题。
第二列的 ItemsSource 取决于第一列选择的值。第一列的值 (SelectedValue) 作为 MethodParameter 传递给 ObjectDataProvider。 ObjectDataProvider 是第二列的 ItemsSource。
使用第一列的 SelectedValue 作为 ObjectDataProvider 的 MethodParameter 的问题是当我在 DataGrid 中插入第二行时。如果第二行在第 1 列中使用的值与在第一行的第 1 列中使用的值不同,则会清除第一行的第 2 列值(因为新的 SelectedValue 更改了允许的可供选择的项目列表,由 ObjectDataProvider 提供)。
我真的很想将第一列的 Text 值作为它的 MethodParameter 传递给 ObjectDataProvider,但是当第一列的 Text 值已经绑定到更新我的模型时,我该怎么做呢?
这是我的问题 XAML 的摘录:
<!--
My ObjectDataProvider. It returns a collection of strings for the user to choose from via the second DataGridTemplateColumn.
The first DataGridTemplateColumn feeds ObjectDataProvider a MethodParameter.
The method is simple. It looks like:
public List<String> ProductLineCategoryList_CategoryCodes(string productLineCode)
{
// return a list of strings based from an object collection, filtered by the passed in argument.
}
-->
<ObjectDataProvider x:Key="categoryCodes" ObjectType="{x:Type e:ItemsProvider}" MethodName="ProductLineCategoryList_CategoryCodes">
<ObjectDataProvider.MethodParameters>
<x:StaticExtension Member="sys:String.Empty"/>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
<!--
This DataGridComboBoxColumn lets the user choose a ProductLineCode.
Its SelectedValue provides a string value for the ObjectDataProvider's MethodParameter.
The ObjectDataProvider is used as the ItemsSource for the DataGridComboBoxColumn
below this one.
The problem with using SelectedValue to feed ObjectDataProvider a MethodParameter, when
a second row is added to my DataGrid and the second row uses a different ProductLineCode than
the first row, it clears the first row's ProductLineCategoryCode value.
-->
<DataGridTemplateColumn
Header="Product Line"
ClipboardContentBinding="{Binding ProductLineCode}">
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox
IsEditable="True"
ItemsSource="{x:Static e:ItemsProvider.ProductLineCategoryList_ProductLineCodeList}"
SelectedValue="{Binding Source={StaticResource categoryCodes},
Path=MethodParameters[0], BindsDirectlyToSource=True,
UpdateSourceTrigger=PropertyChanged}"
Text="{Binding ProductLineCode, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding ProductLineCode}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<!--
This DataGridComboBoxColumn uses the ObjectDataProvider for its ItemsSource.
ItemsSource s/b limited by the selection made from the above DataGridComboBoxColumn.
-->
<DataGridTemplateColumn
Header="Product Line Cat"
ClipboardContentBinding="{Binding ProductLineCategoryCode}">
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox
IsEditable="True"
ItemsSource="{Binding Source={StaticResource categoryCodes}}"
Text="{Binding ProductLineCategoryCode, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding ProductLineCategoryCode}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
我参考了网络寻求帮助,但找不到适合我的解决方案。我只需要传入一个字符串,而不是一个对象(尽管我想我可以更改我的 ObjectDataProvider 的方法来接受一个对象和then this might work)。如果这不是 DataGrid,这个 MSDN solution 会很好用。
【问题讨论】:
标签: c# wpf mvvm wpfdatagrid