DataGrid 数据绑定/更新中的 WPF ComboBox 不起作用 [英] 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.
这篇关于DataGrid 数据绑定/更新中的 WPF ComboBox 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!