DataGridTemplateColumn中的WPF AutoCompleteBox编辑问题

时间:2013-11-17 14:15:32

标签: c# wpf datagrid autocompletebox

我正在尝试使用AutoCompleteBox(在WPFToolkit中找到)而不是DataGridTemplateColumn中的纯文本框,但它似乎不更新绑定的源。

事实上,当我修改文本并按下回车或在单元格外单击时,文本将恢复为原始属性值。

这是一个重现问题的小型WPF应用程序(框架4.0) (它需要引用以下wpf toolkit程序集:
WPFToolkit.dll
System.Windows.Controls.Input.Toolkit.dll

MainWindows.xaml:

<Window x:Class="WpfApplicationTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit"
        Title="MainWindow" Height="350" Width="525"
        x:Name="wnd">
    <StackPanel>
        <DataGrid ItemsSource="{Binding Path=Items, ElementName=wnd}" 
                  AutoGenerateColumns="False">
            <DataGrid.Columns>

                <DataGridTextColumn Header="Item (textcolumn)" Binding="{Binding Name}" />

                <DataGridTemplateColumn Header="Item (autocompletebox)" >
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel>
                                <TextBlock Text="{Binding Name}"/>
                            </StackPanel>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                    <DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <StackPanel>
                                <controls:AutoCompleteBox 
                                    Text="{Binding Name}" 
                                    ItemsSource="{Binding Path=Hints, Source={x:Reference wnd}}"  
                                    Populating="ACBox_Populating" />
                            </StackPanel>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellEditingTemplate>
                </DataGridTemplateColumn>

            </DataGrid.Columns>
        </DataGrid>
    </StackPanel>
</Window>

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    public class Item
    {
        public string Name { get; set; }
    }

    public MainWindow()
    {
        this.Hints = new ObservableCollection<string>();
        this.Items = new Item[]{ new Item{ Name = "a" }, new Item{ Name = "b" }};
        InitializeComponent();
    }

    public Item[] Items { get; set; }

    public ObservableCollection<string> Hints { get; set; }

    private void ACBox_Populating(object sender, PopulatingEventArgs e)
    {
        // generate 10 fake suggestions...
        this.Hints.Clear();
        for (int i = 0; i < 10; i++)
            this.Hints.Add(e.Parameter + "_" + i);
    }
}

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

DataGrid绑定的默认行为是UpdateSourceTrigger = LostFocus将其更改为PropertyChanged。

         <controls:AutoCompleteBox 
                Text="{Binding Name, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" 
                ItemsSource="{Binding Path=Hints, Source={x:Reference wnd}}"  
                Populating="ACBox_Populating" />