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
670
ComboBoxItemsProvider : binding to the current context
posted

Hello,

Inside a UserControl I would like to populate a XamComboEditor using ComboBoxItemsProvider (the ComboBox is in a grid), according to the current context.

The context is defined through an ObjectDataProvider in the parent UserControl.
The problem is that the ComboEditor remains empty as I'm not defining explicitely the source for the binding.
Of course I tested with a simple XamComboEditor outside the grid and without using ComboBoxItemsProvider, it's working perfectly.

Here's my code to define the style (it's inside the resources of the XamDataGrid) :

<Style x:Key="ModuleFieldStyle" TargetType="{x:Type igEditors:XamComboEditor}">
 <Setter Property="ItemsProvider">
  <Setter.Value>
   <igEditors:ComboBoxItemsProvider ItemsSource="{Binding Path=Modules}" DisplayMemberPath="lblModule"/>
  </Setter.Value>
 </Setter>
</Style>

 

 

 

Thanks for your help

  • 54937
    Verified Answer
    Offline posted

    Part of the problem is that the ComboBoxItemsProvider does not have access to the datacontext of the combobox (since that would require an inheritance context and that is internal to wpf currently as well as available to certain types like freezable which really isn't an appropriate base class for comboboxitemsprovider). One option would be to instead set the ItemsSource property of the XamComboEditor since the control has access to its data context. However you still cannot set the binding path you have since the datacontext for the editor element in the cell will be the containing record (since itemscontrol type controls change the datacontext to be that of the item that it represents). Even if you were to go down this path this wouldn't be the most effecient way since every editor instance would be creating its own copy of the items which defeats the major benefit of using the XamComboEditor/ComboBoxItemsProvider (this would actually happen if your snippet worked as well since each editor would have an instance of the items provider). Probably the best solution is to put an instance of the ComboBoxItemsProvider into the resources of the xamDataGrid and use something like Josh's DataContextSpy as the source when binding the ItemsSource of the ComboBoxItemsProvider. e.g.

    <igDP:XamDataGrid BindToSampleData="True">
        <igDP:XamDataGrid.Resources>
            <local:DataContextSpy x:Key="spy" />
            <igEditors:ComboBoxItemsProvider 
                x:Key="ip" 
                ItemsSource="{Binding Path=DataContext.List, 
                    Source={StaticResource spy}}" />
            <Style x:Key="comboStyle" TargetType="igEditors:XamComboEditor">
                <Setter Property="ItemsProvider" Value="{StaticResource ip}" />
            </Style>
        </igDP:XamDataGrid.Resources>
        <igDP:XamDataGrid.FieldLayouts>
            <igDP:FieldLayout>
                <igDP:FieldLayout.Fields>
                    <igDP:Field Name="department">
                        <igDP:Field.Settings>
                            <igDP:FieldSettings 
                                EditorStyle="{StaticResource comboStyle}" />
                        </igDP:Field.Settings>
                    </igDP:Field>
                </igDP:FieldLayout.Fields>
            </igDP:FieldLayout>
        </igDP:XamDataGrid.FieldLayouts>
    </igDP:XamDataGrid>