2

I want to populate a combobox control items from an object data provider. The object data provider calls a method on my custom class to get the list of items.

However the method on my custom class expects a parameter before it can return list of items back. I am not sure how to do it.

Here is my attempt:

Object Data Provider declaration:

<ObjectDataProvider x:Key="dataFromEnum" ObjectType="{x:Type ns:MyDataProvider}" MethodName="GetData">
            <ObjectDataProvider.MethodParameters>
                <sys:String>String.Empty</sys:String>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>

ComboBox declaration:

<ComboBox Name="combobox" ItemsSource="{StaticResource dataFromEnum}" Tag="{Binding Path=Name}" />

So as you can see the combo box's Tag property is bound to the Name property of the parent's data context. I want to pass that Name property to the object data provider. I am not sure how to pass the data into the object data provider while requesting data from it.

Please advise.

Thanks.

H.B.
  • 166,899
  • 29
  • 327
  • 400
user1647697
  • 59
  • 1
  • 7

2 Answers2

0

There are examples of binding to the method parameters to adjust their value, but that's not really applicable here...

Faced with this challenge, why not have the ViewModel expose a property to bind to the ItemsSource for the combo control?

Often I'd have something like:

public MySomething SelectedSomething { get; set; }
public IEnumerable<MySomething> AvailableSomethings
{
  get { /* Access a factory method or something to return applicable list for this view model state */ }
}

Then the binding becomes:

<ComboBox ItemsSource="{Binding path=AvailableSomethings}" SelectedItem="{Binding path=SelectedSomething}"/>
Steve Py
  • 26,149
  • 3
  • 25
  • 43
  • Well, I dont think I can do that. This combobox is being rendered inside of a grid control. The grid control itself is bound to an IEnumerable. So there will be multiple rows and each row will have a combobox. Each combobox might have to render a different set of options inside depending on the type. So thats the complexity. I cannot defined a property in my view model as you described since the combo boxes are generated per row in the grid. And I dont know how many rows (so how many combo boxes). – user1647697 Sep 27 '12 at 21:45
0

in wpf you have to generate event for selection changed for that you have to give references

clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity

 <ComboBox ItemsSource="{Binding UserSectionItems, Mode=TwoWay}" Margin="104,11,0,10" HorizontalAlignment="Left" Width="135" Name="UserCmd" >
            <l:Interaction.Triggers>
                <l:EventTrigger EventName="SelectionChanged">
                    <l:InvokeCommandAction Command="{Binding UserComboBoxSelectionCmd, Mode=TwoWay}" CommandParameter="{Binding ElementName=UserCmd,Path=SelectedItem}"/>
                </l:EventTrigger>
            </l:Interaction.Triggers>
        </ComboBox>

in your Viewmodel look like

public RelayCommand<string> UserComboBoxSelectionCmd { get; set; }
Dhaval Patel
  • 7,471
  • 6
  • 37
  • 70
  • Please see my comment above. My comment applies to your solution as well. I cannot do it the way you described. – user1647697 Sep 27 '12 at 21:46