2016-11-08 57 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

那么我有一些可扩展内容下的可编辑文本框和项目相关的菜单,我发布的问题不会让人们对重载信息感到困惑。要清楚:我只想获得项目上下文而不是扩展器内的列表上下文。感谢您的帮助顺便说一句。 –