0
votes

I have difficulties in binding the follow structures to XAML View:

public class SampleViewModel : ViewModelBase
{
   public ObservableCollection<ChildViewModel> Child { get; set; }
   ...
   public ObservableCollection<Country> Countries { get; set; }
   ...
}
public class ChildViewModel : ViewModelBase
{
   private int _CountryId;
   public int CountryId
   {
      get { return _CountryId; }
      set
      {
         _CountryId = value;
         OnPropertyChanged("CountryId");
      }
   }
   ...
}
// Country structure is not shown, just an int and string for CountryID 
// and Name in this case

Instance of SampleViewModel is set as DataContext of a View. I bind the collection Child to a GridView ItemsSource. In the GridView I have a ComboBox for Country and I want to populate it with Countries collection in the SampleViewModel.

<Telerik:RadGridView ItemsSource="{ Binding Child }" ...>
   <Telerik:RadGridView.Columns>
      <Telerik:GridViewComboBoxColumn DataMemberBinding="{Binding CountryId}"
                                      SelectedValueMemberPath="Id"
                                      DisplayMemberPath="Name"
                                      ItemsSource="{Binding ????}" />
      ...
   ...
...

What should be the ItemsSource ? How can I go back to root VM's properties inside a ItemsSource="{ Binding Child }" ?

Or how should I restructure the ViewModel to achieve the above ?

1

1 Answers

1
votes

I am not sure how if this works with the Telerik controls, but usually you declare the binding via the RelativeSource attached property:

{Binding Path=DataContext.Countries, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Telerik:RadGridView}}}

Another way is to set a name on your RadGridView and use that as ElementName:

<Telerik:RadGridView ItemsSource="{ Binding Child }" x:Name="gridView">
...
      <Telerik:GridViewComboBoxColumn DataMemberBinding="{Binding CountryId}"
                                      SelectedValueMemberPath="Id"
                                      DisplayMemberPath="Name"
                                      ItemsSource="{Binding DataContext.Countries, ElementName=gridView}" />