Question

I'm trying to use an AutoCompleteBox (found in WPFToolkit) instead of a plain TextBox inside a DataGridTemplateColumn, but it doesn't seem to update the bound source.

In fact, when I modify the text and press enter or click outside the cell, the text revert back to the original property value.

Here's a small WPF application (framework 4.0) that reproduces the problem
(it needs a reference to the following wpf toolkit assemblies:
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);
    }
}

Any idea ?

Was it helpful?

Solution

A DataGrid's default behavior for binding is UpdateSourceTrigger=LostFocus change it to PropertyChanged.

         <controls:AutoCompleteBox 
                Text="{Binding Name, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" 
                ItemsSource="{Binding Path=Hints, Source={x:Reference wnd}}"  
                Populating="ACBox_Populating" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top