2011-06-08 31 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,而不是數據對象。