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;
}
}
这种结合工作,但问题是,它结合了账户内的所有物品。事情是,我只想将每个项目绑定到不同的expanderHeader。我的意思是,这种绑定是像DataGridSource =帐户但我想要的东西像DataGridSource =帐户 –
@stackerflow,我重读这个问题,并感到困惑为什么ItemTemplate包含Expander和DataGrid的(在标题)项目是一个简单的用户只有2个属性的对象。也许使用DataGrid而不是ItemsControl?像这样: ' –
ASh
那么我有一些可扩展内容下的可编辑文本框和项目相关的菜单,我发布的问题不会让人们对重载信息感到困惑。要清楚:我只想获得项目上下文而不是扩展器内的列表上下文。感谢您的帮助顺便说一句。 –