我有一个带有 ComboBox 的 DataGrid 模板列。当我选择一个值并按 Enter 时,绑定的数据不会更新(我看到空单元格)。
XAML:
<Window x:Class="WpfGrid2.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:dg="clr-namespace:Microsoft.Windows.Controls;assembly=WpfToolkit"
>
<Window.Resources>
<x:Array x:Key="people" Type="sys:Object" />
<x:Array x:Key="knownLastNames" Type="sys:String">
<sys:String>Smith</sys:String>
<sys:String>Johnson</sys:String>
<sys:String>Williams</sys:String>
</x:Array>
</Window.Resources>
<StackPanel>
<dg:DataGrid x:Name="_grid" ItemsSource="{DynamicResource people}" CanUserAddRows="True" AutoGenerateColumns="False">
<dg:DataGrid.Columns>
<dg:DataGridTemplateColumn Header="LastName" MinWidth="100">
<dg:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox ItemsSource="{DynamicResource knownLastNames}" SelectedItem="{Binding LastName}"></ComboBox>
</DataTemplate>
</dg:DataGridTemplateColumn.CellEditingTemplate>
<dg:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding LastName}" />
</DataTemplate>
</dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>
</dg:DataGrid.Columns>
</dg:DataGrid>
<Button>test</Button>
</StackPanel>
</Window>
代码隐藏:
namespace WpfGrid2
{
public partial class Window2 : Window
{
public Window2()
{
InitializeComponent();
List<Person> people = new List<Person>();
this.Resources["people"] = people;
}
}
}
如果我将 ComboBox 更改为 TextBox,它可以正常工作
<TextBox Text="{Binding LastName}" />
怎么了?