WPF ComboBox在DataGrid数据绑定/更新中不起作用 [英] WPF ComboBox in DataGrid databinding/update not working
问题描述
如果我在Visual Studio 2010中设置了新的WPF应用程序并添加了以下代码+ XAML,则将打开一个数据网格,其中包含组合框。现在的问题是,通过组合框更改值不会传播到绑定的数据模型。换句话说:永远不会设置名为MyValue的属性。现在花了我几个小时,我不知道为什么这行不通。
If I set up a new WPF application in Visual Studio 2010 and add the following code+XAML, a data grid opens with comboboxes inside. The problem now is that changeing a value through a combobox does not get propagated to the bound data model. In other words: the property named MyValue never gets set. It took me hours now and I have no clue why this doesn't work. Also many similar threads and suggestions didn't.
这里是XAML。它只是一个包含DataGrid的简单窗口。 DataGrid有一个模板列,其中设置了CellTemplate和CellEditingTemplate。两者都包含一个ComboBox,其中填充了资源部分中的列表。 ComboBox.SelectedItem绑定到MyItem.MyValue:
Here the XAML. It is just a simple Window with a DataGrid contained. The DataGrid has a template column where CellTemplate and CellEditingTemplate are set. Both contain a ComboBox that is filled with the list from the resource section. The ComboBox.SelectedItem is bound to MyItem.MyValue:
<Window x:Class="DataGridComboBoxExample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" xmlns:local="clr-namespace:DataGridComboBoxExample">
<Window.Resources>
<local:MyItemList x:Key="ItemList"/>
<DataTemplate x:Key="NotificationModeDataTemplate">
<ComboBox
ItemsSource="{StaticResource ItemList}"
SelectedItem="{Binding Path=MyValue, Mode=OneWay}" />
</DataTemplate>
<DataTemplate x:Key="NotificationModeEditTemplate">
<ComboBox
ItemsSource="{StaticResource ItemList}"
SelectedItem="{Binding Path=MyValue, Mode=TwoWay}" />
</DataTemplate>
</Window.Resources>
<Grid>
<DataGrid x:Name="myDataGrid" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn
Header="Test" Width="100"
CellTemplate="{StaticResource NotificationModeDataTemplate}"
CellEditingTemplate="{StaticResource NotificationModeEditTemplate}" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
此处是代码。它包含仅设置DataContext的主Window ctor。 MyItem是该行的数据模型,支持INotifyPropertyChanged。 MyItemList是绑定到ComboBox.ItemsSource的选项的列表。
Here the code. It contains the main Window ctor which just sets up a DataContext. MyItem is the row's datamodel supporting INotifyPropertyChanged. MyItemList is the list of choices bound to ComboBox.ItemsSource.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
myDataGrid.ItemsSource = new List<MyItem>
{
new MyItem { MyValue = "i0" },
new MyItem { MyValue = "i1" },
new MyItem { MyValue = "i0" },
};
}
}
public class MyItem : INotifyPropertyChanged
{
public string MyValue
{
get { return myValue; }
set
{
myValue = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("MyValue"));
}
}
}
private string myValue;
public event PropertyChangedEventHandler PropertyChanged;
}
public class MyItemList : List<string>
{
public MyItemList() { Add("i0"); Add("i1"); Add("i2"); }
}
推荐答案
我怀疑您会需要使SelectedItem绑定更新PropertyChanged上的源才能起作用。
I suspect you'll need to make the SelectedItem binding update the source on PropertyChanged for this to work.
SelectedItem = {Binding Path = MyValue,Mode = TwoWay, UpdateSourceTrigger = PropertyChanged }
SelectedItem="{Binding Path=MyValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
我会在调试此模板时让CellTemplate和CellEditingTemplate都引用您的编辑模板,以消除其他无关的模板,直到获得它为止整理出来。
I'd make the CellTemplate and CellEditingTemplate both reference your edit template while debugging this, to eliminate the other, irrelevant, template until you get it sorted out.
这篇关于WPF ComboBox在DataGrid数据绑定/更新中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!