2016-11-08 59 views
1

我想獲取祖先ItemsControl.ItemTemplate源作爲我的DataGrid源,但我無法處理這種情況。我在我的xaml中嘗試了以下情況。但它沒有用,datagrid是空的。在Wpf中獲取祖先源

這是我的XAML:

<ItemsControl ItemsSource="{Binding Accounts}"> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <Expander ExpandDirection="Down" FlowDirection=" RightToLeft"> 
       <Expander.Header> 
        <DataGrid FlowDirection="LeftToRight" 
          ItemsSource="{RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemTemplate}}}"> 
         <DataGrid.Columns> 
          <DataGridTextColumn Binding="{Binding UserName}" /> 
          <DataGridTextColumn Binding="{Binding Password}" /> 
         </DataGrid.Columns> 
        </DataGrid> 
       </Expander.Header> 
      </Expander> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 

帳戶屬性實現像:

public List<User> Accounts = new List<User>(); 
Accounts.Add(new User("userName1","password1")); 
Accounts.Add(new User("userName2","password2")); 

而且我User.cs是這樣的:

public class User : INotifyPropertyChanged 
{ 
     public string UserName{get; set;} 
     public string Password{get; set;} 


     public UserAccount(string userName, string password) 
     { 
      UserName = userName; 
      Password = password; 
     } 
} 

回答

3

基本上,此代碼不cr eate a binding

ItemsSource="{RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemTemplate}}}" 

也是祖先類型不正確。

嘗試這個綁定:

ItemsSource="{Binding Path=DataContext.Accounts, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}}" 

,或者使用的ElementName:

<ItemsControl Name="LstAccounts" ItemsSource="{Binding Accounts}"> 

ItemsSource="{Binding Path=DataContext.Accounts, ElementName=LstAccounts}" 

代碼以上修復結合,但產生意想不到的效果。這裏是表示單個對象在數據網格

爲的ItemSource結合我創建了一個轉換器(my own older answer

public class ItemsSourceConverter: IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     // doesn't allow to add new rows in DataGrid 
     return Enumerable.Repeat(value, 1).ToArray();    
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

和這裏被修改ItemsControl的溶液。轉換器存儲在資源中並用於ItemsSource綁定。 DataGrid中自動生成的實例列

<ItemsControl ItemsSource="{Binding Accounts}"> 
    <ItemsControl.Resources> 
     <local:ItemsSourceConverter x:Key="Enumerator"/> 
    </ItemsControl.Resources> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <Expander ExpandDirection="Down" FlowDirection=" RightToLeft"> 
       <Expander.Header> 
        <DataGrid FlowDirection="LeftToRight" 
           ItemsSource="{Binding ., Converter={StaticResource Enumerator}}"/> 
       </Expander.Header> 
      </Expander> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

這裏是截圖

it looks like this

+0

這種結合工作,但問題是,它結合了賬戶內的所有物品。事情是,我只想將每個項目綁定到不同的expanderHeader。我的意思是,這種綁定是像DataGridSource =帳戶但我想要的東西像DataGridSource =帳戶 –

+0

@stackerflow,我重讀這個問題,並感到困惑爲什麼ItemTemplate包含Expander和DataGrid的(在標題)項目是一個簡單的用戶只有2個屬性的對象。也許使用DataGrid而不是ItemsControl?像這樣: ' – ASh

+0

那麼我有一些可擴展內容下的可編輯文本框和項目相關的菜單,我發佈的問題不會讓人們對重載信息感到困惑。要清楚:我只想獲得項目上下文而不是擴展器內的列表上下文。感謝您的幫助順便說一句。 –