2011-06-08 67 views
1

我正在尝试使用项目列表(ParentCredentials)填充组合框,它是项目控件的一部分。问题是这些ParentCredentials与itemscontrol绑定的项目处于同一级别。不知道这是明确的,但如果你看看视图模型应该更有意义在项目控件中绑定组合框

这是我的视图模型:

public class AccessControlViewModel : INotifyPropertyChanged 
    { 
public ObservableCollection<LogonCredential> Credentials 
     {...} 
    public List<string> ParentCredentials 
     {...} 
} 

,我有以下XAML。

<ItemsControl ItemsSource="{Binding AccessControl.Credentials}" HorizontalContentAlignment="Stretch"> 
     <ItemsControl.ItemTemplate> 
     <DataTemplate>        
      <Grid > 
       <Grid.ColumnDefinitions > 
        <ColumnDefinition Width="*" /> 
        <ColumnDefinition Width="*"/> 
        <ColumnDefinition Width="*"/> 
       </Grid.ColumnDefinitions>          

      <Label Grid.Column="0" Content="{Binding Path=DisplayName}"/> 
      <ComboBox Grid.Column="2" ItemsSource="{Binding Source={RelativeSource AncestorType={x:Type vm:ResourceViewModel}}, Path=AccessControl.ParentCredentials}">           
      </ComboBox> 
      ... 

我该如何做这个绑定?另请注意,AccessControl是ResourceViewModel类的一部分。

回答

2

您需要导航回ItemsControl,并通过DataContext路径进行绑定。

{Binding RelativeSource={RelativeSource AncestorType=ItemsControl}, Path=DataContext.AccessControl.ParentCredentials} 

Source={RelativeSource...从来没有在任何情况下工作。此外,AncestorType始终是一些FrameworkElement,而不是数据对象。