Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
315
Help: XamComboEditor doesn't update label?
posted

Hi,

Here's my setup:

  1. XamComboEditor
    • ItemsSource bound to collection of objects (ItemViewModel)
    • DisplayMemberPath set to a label property (ItemViewModel.ItemLabel)
  2. ItemViewModel.ItemLabel
    •  Raises PropertyChanged event when label changes

 

And here's my problem:

  1. When ItemViewModel.ItemLabel changes
    1. If cell is not in edit mode, the displayed value does not update
    2. If cell is in edit mode (ie, when drop down is shown), the updated value is shown
    3. When I exit edit mode, the displayed label reverts to the old value again.
  2. If I use a custom template (ComboBox) for XamComboEditor.Template, it works fine.

Am I missing a setting? I would like to continue using XamComboEditor's default template if possible.

 

  • 138253
    Offline posted

    Hello,

     

    It has been a while since you have made your post, in case you still need support I will be glad to assist you further. I suppose the other community members can benefit from this answer as well. I have been looking into your post and I created a sample project for you where the functionality you want is achieved. Basically after changing the DataSource of the XamComboEditor I iterated trough the Records and make the Cell with the Editor to enter and exit edit mode and I handled the EditModeEnding event, so I can change the Editor’s Value to the updated one.

     

    Feel free to write me if you have further questions.

    xamDataGridComboEditorSource.zip
  • 315
    posted

    Here's the source code:

    MainWindow.xaml

    <Window x:Class="XamGridTest.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
            xmlns:igd="http://infragistics.com/DataPresenter" 
            xmlns:Editors="http://infragistics.com/Editors" 
            Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
            <ControlTemplate TargetType="{x:Type Editors:XamComboEditor}" x:Key="ComboEditorTemplate">
                <ComboBox ItemsSource="{TemplateBinding ItemsSource}"
                          SelectedItem="{TemplateBinding SelectedItem}"
                          DisplayMemberPath="{TemplateBinding DisplayMemberPath}" />
                          
            </ControlTemplate>
        </Window.Resources>
        <DockPanel>
            <!-- Editor for item labels -->
            <ListBox DockPanel.Dock="Left" Width="200" x:Name="lstLabelEditor" ItemsSource="{Binding ItemCollection}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding ItemLabel, Mode=TwoWay}" />
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
            
            <!-- Xam data binding -->
            <igd:XamDataGrid x:Name="xdgDataGrid" DataSource="{Binding Records}"
                                FieldLayoutInitialized="OnFieldLayoutInitialized">
                <igd:XamDataGrid.FieldLayoutSettings>
                    <igd:FieldLayoutSettings AutoGenerateFields="False" />
                </igd:XamDataGrid.FieldLayoutSettings>
            </igd:XamDataGrid>
        </DockPanel>
    </Window>
    
    
    

     

    MainWindow.xaml.cs

    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Windows;
    using System.Windows.Data;
    using Infragistics.Windows.DataPresenter;
    using Infragistics.Windows.DataPresenter.Events;
    using Infragistics.Windows.Editors;
    
    namespace XamGridTest
    {
        public partial class MainWindow
        {
            private readonly MainViewModel _mainViewModel = new MainViewModel();
    
            public MainWindow()
            {
                InitializeComponent();
                DataContext = _mainViewModel;
            }
    
            private void OnFieldLayoutInitialized(object sender, FieldLayoutInitializedEventArgs e)
            {
                var layout = e.FieldLayout;
                layout.Fields.Clear();
    
                // Field using XamComboEditor's default template
                var comboField1 = new UnboundField
                                      {
                                          DataType = typeof (ItemViewModel),
                                          Label="Default template",
                                          Binding = new Binding("RecordItem") {Mode = BindingMode.TwoWay}
                                      };
                layout.Fields.Add(comboField1);
                comboField1.Settings.EditorType = typeof(XamComboEditor);
                Style style1 = new Style(typeof(XamComboEditor));
                {
                    style1.Setters.Add(new Setter(XamComboEditor.ItemsSourceProperty, _mainViewModel.ItemCollection));
                    style1.Setters.Add(new Setter(XamComboEditor.DisplayMemberPathProperty, "ItemLabel"));
                    comboField1.Settings.EditorStyle = style1;
                }
    
                // Field using XamComboEditor with custom template
                var comboField2 = new UnboundField
                                      {
                                          DataType = typeof (ItemViewModel),
                                          Label = "Custom template",
                                          Binding = new Binding("RecordItem") {Mode = BindingMode.TwoWay}
                                      };
                layout.Fields.Add(comboField2);
                comboField2.Settings.EditorType = typeof(XamComboEditor);
                Style style2 = new Style(typeof (XamComboEditor));
                {
                    style2.Setters.Add(new Setter(XamComboEditor.ItemsSourceProperty, _mainViewModel.ItemCollection));
                    style2.Setters.Add(new Setter(XamComboEditor.DisplayMemberPathProperty, "ItemLabel"));
                    style2.Setters.Add(new Setter(TemplateProperty, FindResource("ComboEditorTemplate")));
                    comboField2.Settings.EditorStyle = style2;
                }
            }
        }
    
        // Main view model
        public class MainViewModel
        {
            private readonly ObservableCollection _itemCollection =
                new ObservableCollection
                    {
                        new ItemViewModel{ItemLabel="Default item 1"},
                        new ItemViewModel{ItemLabel="Default item 2"},
                        new ItemViewModel{ItemLabel="Default item 3"},
                    };
            public ObservableCollection ItemCollection
            {
                get { return _itemCollection; }
            }
    
            private readonly ObservableCollection _records;
            public ObservableCollection Records
            {
                get { return _records; }
            }
    
            public MainViewModel()
            {
                _records = new ObservableCollection
                               {
                                   new RecordViewModel{RecordItem = _itemCollection[0]},
                                   new RecordViewModel{RecordItem = _itemCollection[1]},
                                   new RecordViewModel{RecordItem = _itemCollection[2]},
                                   new RecordViewModel{RecordItem = _itemCollection[0]},
                                   new RecordViewModel{RecordItem = _itemCollection[1]},
                                   new RecordViewModel{RecordItem = _itemCollection[2]},
                               };
            }
        }
    
        // Record view model
        public class RecordViewModel : INotifyPropertyChanged
        {
            private ItemViewModel _recordValue;
            public ItemViewModel RecordItem
            {
                get { return _recordValue; }
                set { _recordValue = value;
                    if (PropertyChanged != null)
                        PropertyChanged(this, new PropertyChangedEventArgs("RecordItem"));
                }
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
        }
    
        // Item view model
        public class ItemViewModel : INotifyPropertyChanged
        {
            private string _itemLabel;
            public string ItemLabel
            {
                get { return _itemLabel; }
                set
                {
                    _itemLabel = value;
                    if (PropertyChanged != null)
                        PropertyChanged(this, new PropertyChangedEventArgs("ItemLabel"));
                }
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
        }
    }