【发布时间】:2017-03-21 18:17:37
【问题描述】:
我正在尝试将祖先 ItemsControl.ItemTemplate 源作为我的 DataGrid 源,但我无法处理这种情况。我在我的 xaml 中尝试了以下案例。但它不起作用,datagrid 是空的。
这是我的 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>
Accounts 属性实现如下:
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;
}
}
【问题讨论】:
标签: c# wpf xaml binding datacontext