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
20
Combobox in XamDataGrid - Programmatically
posted

Hello,

I’m new to WPF so what I’m asking may be simple it’s just that I’m inexperienced and may not know what I need to do.

 

Basically, I have a XamDataGrid control that is used to edit a collection of business/domain objects.  The object being edited is not known until runtime.  The object contains enough metadata to create my field layout and assign the correct data types, positions, etc.  I’m having problems with lookup/combox types though.  A developer can assign a lookup “Collection<KeyValuePair<TKey, TValue>>” to the business/domain class and the application/grid is supposed to use these values exclusively.

I’ve seen plenty of examples on how to do this though Xaml, but, I cannot figure out how to do this programmatically, although I think I’m getting closer.

What I need to accomplish.

1.       Binding my business/domain object to the grid.

2.       Allow for updating/editing/adding.

3.       Bind certain fields to a combox and have the underlying data source (business object) reflect the changes.

4.       Must be done at runtime though code.

So far I have the following code (only relevant code is posted) but my behavior is not correct.  Firstly, the combox itemsource is set in the CreateLookup function, but my bindings are not being set correctly.   Secondly, I had to use the Loaded event to set my bindings otherwise the dropdown contained the “ToString()” value “[B, Business], [I,Individual]” etc. Thirdly, the combobox does display, but when the first selector (arrow) is clicked a combox appears and then I have to click it again.  I like the look of the original selector and I would like to keep it as the default and just display my contents at that point. 

I’m not opposed to using a resourced style in xaml as long as I can change the source value at runtime.  I tried that as well (see xaml snippet at the end of the posting).   This is actually ideal, I just couldn’t figure out how to make it work correctly.

Any assistance would be greatly appreciated; I’ve been stuck on this single item for 3 days now.  If you need any additional information please let me know and I’ll be sure to reply.

TIA,

Jared

        private void BindStagingRecords()

        {            if (this.Importer != null)            {                this.gridStagingItems.DataSource = (IEnumerable)this.Importer.Items;                FieldLayout layout = this.gridStagingItems.FieldLayouts["standard"];                layout.Fields.Clear();                if (layout != null)                {                    List<StagingColumn>.Enumerator iterator = this.Importer.Columns.GetEnumerator();                    while (iterator.MoveNext() == true)                    {                        Field field = new Field()                        {                            Name = iterator.Current.ColumnName,                            DataType = iterator.Current.DataType,                            Label = iterator.Current.DisplayName,                            Column = iterator.Current.OrdinalPosition,                            Visibility = (Visibility)((iterator.Current.IsVisible) ? Visibility.Visible : Visibility.Collapsed)                        };                        if (iterator.Current.HasLookup == true)                        {                            field.Settings.EditorStyle = this.CreateLookup(iterator.Current);                        }                        layout.Fields.Add(field);                    }                }            }        }         private Style CreateLookup(StagingColumn column)        {            Style style = new Style(typeof(XamComboEditor));            ControlTemplate template = new ControlTemplate(typeof(XamComboEditor));            style.Setters.Add(new Setter(XamComboEditor.EditTemplateProperty, template));            FrameworkElementFactory content = new FrameworkElementFactory(typeof(XamComboEditor));            content.SetBinding(XamComboEditor.ItemsSourceProperty, new Binding() { Source = column.Lookup });            content.SetBinding(XamComboEditor.DisplayMemberPathProperty, new Binding("Value") { RelativeSource = new RelativeSource(RelativeSourceMode.Self) });            content.SetBinding(XamComboEditor.TextProperty, new Binding("Value") { RelativeSource = new RelativeSource(RelativeSourceMode.Self) });            content.SetBinding(XamComboEditor.ValuePathProperty, new Binding("Key") { RelativeSource = new RelativeSource(RelativeSourceMode.Self) });            content.SetBinding(XamComboEditor.SelectedItemProperty, new Binding("Key") { RelativeSource = new RelativeSource(RelativeSourceMode.Self) });             content.AddHandler(XamComboEditor.LoadedEvent, new RoutedEventHandler(test));            template.VisualTree = content;             return style;        }        private void test(object sender, RoutedEventArgs e)        {             XamComboEditor editor = sender as XamComboEditor;            if (editor != null)            {                editor.DisplayMemberPath = "Value";                editor.ValuePath = "Key";            }

        }

             <Grid.Resources>                  <Style TargetType="{x:Type igEditors:XamComboEditor}" x:Key="customComboBoxEditor">                        <Setter Property="EditTemplate">                              <Setter.Value>                                    <ControlTemplate TargetType="{x:Type igEditors:XamComboEditor}">                                          <StackPanel>                                                <ComboBox x:Name="cboComboboxEditor" Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Text, Mode=TwoWay}"                                                      IsEditable="False"                                                      SelectedItem="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Text, Mode=TwoWay }"                                                      DisplayMemberPath="Value" Loaded="test" >                                                </ComboBox>                                          </StackPanel>                                    </ControlTemplate>                              </Setter.Value>                        </Setter>                  </Style> 

            </Grid.Resources>

 

Parents
No Data
Reply
  • 138253
    Offline posted

    Hello jaredwri,

    I have been looking into this for you and I can suggest you use the XamlReader class to easily load the same Style from code and this will also provide you with the opportunity to embed a variable on the spot from code behind. Here is a forum thread:

    http://community.infragistics.com/forums/t/48810.aspx?PageIndex=1

    in which it is discussed how to do so. Please note, we are making efforts to ensure that all posts are addressed by an Infragistics expert. We believe that the other community users could benefit from this thread as well.

    Feel free to write me if you need further assistance.

Children
No Data