在一个itemscontrol中绑定组合框

时间:2011-06-08 11:02:51

标签: c# wpf xaml data-binding combobox

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

这是我的viewmodel:

 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类的一部分。

1 个答案:

答案 0 :(得分:2)

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

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

Source={RelativeSource...在任何情况下都不起作用。此外,AncestorType始终是一些FrameworkElement而不是数据对象。

相关问题