C# 在Wpf中获取祖先源
我正在尝试获取祖先ItemsControl.ItemTemplate源作为我的DataGrid源,但无法处理此情况。我在xaml中尝试了以下情况。但它没有工作,datagrid是空的 这是我的xaml:C# 在Wpf中获取祖先源,c#,wpf,xaml,binding,datacontext,C#,Wpf,Xaml,Binding,Datacontext,我正在尝试获取祖先ItemsControl.ItemTemplate源作为我的DataGrid源,但无法处理此情况。我在xaml中尝试了以下情况。但它没有工作,datagrid是空的 这是我的xaml: <ItemsControl ItemsSource="{Binding Accounts}"> <ItemsControl.ItemTemplate> <DataTemplate> <Expander Ex
<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>
基本上,这段代码不创建绑定
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}">
上面的代码修复了绑定,但产生了意外的结果。下面是一个在DataGrid中显示单个对象的解决方案 对于ItemSource绑定,我创建了一个转换器() 这里是修改后的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>
这是一个截图
此绑定正在工作,但问题是它绑定了帐户中的所有项目。问题是,我只想将每个项目绑定到不同的expanderHeader。我的意思是,此绑定的工作方式类似于DataGridSource=Accounts,但我希望类似于DataGridSource=Account@stackerflow,我重新阅读了这个问题,弄不明白为什么ItemTemplate包含
Expander
和DataGrid
(在标题中),而items是一个只有两个属性的简单用户对象。可能使用DataGrid而不是ItemsControl?像这样:
我在expander content下有一些可编辑的文本框和与项目相关的菜单,我在发布问题时删除了这些文本框和菜单,以避免将人们与过载信息混淆。需要明确的是:我只想获取项目上下文,而不是扩展器中的列表上下文。谢谢你的帮助,顺便说一句,如果标题应该显示单个用户的信息,我不会使用DataGrid(这对我来说真的很困惑,尽管即使对于单个项目也是可能的)。我会使用带有一些控件的面板,例如:
。这当然是原始的,需要改进,比如标题,但显示了想法。重点是StackPanel非常原始,设计它对我来说非常复杂。我只是喜欢数据网格的可视化,这就是我选择它的原因。如果您有任何关于将stackpanel可视化转换为dataGridRow样式的解决方案,那么它也将非常有用
<ItemsControl Name="LstAccounts" ItemsSource="{Binding Accounts}">
ItemsSource="{Binding Path=DataContext.Accounts, ElementName=LstAccounts}"
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="{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>