This project has moved. For the latest updates, please go here.

Editable combobox problem

Jan 13, 2016 at 9:35 AM
Edited Jan 13, 2016 at 9:37 AM
I'm trying to build an editable combobox. This combobox in a datagrid. Binding is made with MVVM.
Everything works well except this : after typing a new value, this value is lost when going out of the combo.

Here is my code :
<xcdg:Column FieldName="FlangeType"  Title="Flange Type"  Width="80" >

    <xcdg:Column.CellEditor>
          <xcdg:CellEditor>
                <xcdg:CellEditor.EditTemplate>
                       <DataTemplate>
                            <ComboBox
                                            ItemsSource="{Binding Path= DataContext.FlangeTypes, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" 
                                            SelectedValue="{xcdg:CellEditorBinding}"
                                            Text="{Binding Path=DataContext.CurrentDrumStandard.FlangeType,UpdateSourceTrigger=LostFocus}"
                                            IsEditable="True"/>
                         </DataTemplate>
                    </xcdg:CellEditor.EditTemplate>
              </xcdg:CellEditor>
      </xcdg:Column.CellEditor>
                        
</xcdg:Column> 
Thanks in advance for any help.
Developer
Jan 15, 2016 at 2:29 PM
I don't have all your code, but using this code, it works :
<Window x:Class="WpfApplication40.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication40"
        xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
        xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
   <Window.Resources>
      <ObjectDataProvider MethodName="GetValues"
                          ObjectType="{x:Type sys:Enum}"
                          x:Key="CombboStylesValues">
         <ObjectDataProvider.MethodParameters>
            <x:Type TypeName="local:ComboValues" />
         </ObjectDataProvider.MethodParameters>
      </ObjectDataProvider>
   </Window.Resources>
    <StackPanel>
      <xcdg:DataGridControl x:Name="_dataGrid"
                            ItemsSource="{Binding MyList}">
         <xcdg:DataGridControl.Columns>
            <xcdg:Column FieldName="Name"/>
            <xcdg:Column FieldName="FlangeType"
                         Title="Flange Type"
                         Width="80">

               <xcdg:Column.CellEditor>
                  <xcdg:CellEditor>
                     <xcdg:CellEditor.EditTemplate>
                        <DataTemplate>
                           <ComboBox ItemsSource="{Binding Source={StaticResource CombboStylesValues}}"
                                     SelectedValue="{xcdg:CellEditorBinding}"
                                     IsEditable="True"/>
                        </DataTemplate>
                     </xcdg:CellEditor.EditTemplate>
                  </xcdg:CellEditor>
               </xcdg:Column.CellEditor>
            </xcdg:Column>
         </xcdg:DataGridControl.Columns>
      </xcdg:DataGridControl>
   </StackPanel>
</Window>
 public enum ComboValues
  {
    One,
    Two,
    Three,
    Four
  }

  public partial class MainWindow : Window
  {
    public MainWindow()
    {
      Xceed.Wpf.Toolkit.Licenser.LicenseKey = "XXXXX-XXXXX-XXXXX-XXXX";

      InitializeComponent();

      this.DataContext = this;

      this.MyList = new List<MyClass>()
      {
        new MyClass() { Name = "First", FlangeType = ComboValues.One },
        new MyClass() { Name = "Second", FlangeType = ComboValues.Three},
        new MyClass() { Name = "Third", FlangeType = ComboValues.Four }
      };
    }
   
    public List<MyClass> MyList
    {
      get;
      set;
    }
  }

  public class MyClass
  {
    public string Name
    {
      get;
      set;
    }

    public ComboValues FlangeType
    {
      get;
      set;
    }
  }
Marked as answer by peltifr1 on 1/15/2016 at 2:56 PM
Jan 15, 2016 at 9:56 PM
Thank you for your answer.

I couldn't get it work in my context without using the"Text" property of the combo. The binding of this property was not correct in my first post ! (missing RelativeSource)

After correction, it's ok for me with the code below.
Now, user can type a new value and this value is correctly propagated to the model.
                                <xcdg:Column FieldName="FlangeType"  Title="Flange Type"  Width="80" >
                                    <xcdg:Column.CellEditor>
                                        <xcdg:CellEditor>
                                            <xcdg:CellEditor.EditTemplate>
                                                <DataTemplate>
                                                    <ComboBox
                                                        ItemsSource="{Binding DataContext.FlangeTypes, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" 
                                                        SelectedValue="{xcdg:CellEditorBinding}"
                                                        __Text="{Binding DataContext.CurrentDrumStandard.FlangeType, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, UpdateSourceTrigger=LostFocus}"
__                                                      IsEditable="True"/>
                                                </DataTemplate>
                                            </xcdg:CellEditor.EditTemplate>
                                        </xcdg:CellEditor>
                                    </xcdg:Column.CellEditor>
                                </xcdg:Column>