2012-02-23 133 views
0

我有约束力的后续结构XAML浏览困难:XAML绑定集合的子项到视图模型根的另一个集合

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 

SampleViewModel的实例设置为视图的DataContext的。我将集合Child绑定到GridView ItemsSource。在GridView中,我有一个ComboBox for Country,我想用SampleViewModel中的Countries集合填充它。

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

什么应该是ItemsSource?我怎样才能回到根目录VM的属性在一个ItemsSource =“{Binding Child}”?

或者我应该如何重构ViewModel才能达到上述目的?

回答

1

我不知道怎么样,如果这个工程与Telerik控制,但通常你声明通过RelativeSource附加属性的绑定:

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

另一种方法是在你的RadGridView设置一个名称,并用其作为元素名称:

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

在我的示例的XAML中修复了我的愚蠢错字之后,ElementName方法起作用。谢谢。 – Lepton 2012-02-23 08:44:17