问题描述
我想填充一个组合框,它是itemcontrol的一部分,有一个项目列表(ParentCredentials)。问题是这些ParentCredentials处于与项目控件绑定的项目的同一级别。不确定这是否清楚,但如果你看看视图模型应该更有意义
I am trying to populate a combobox, which is part of an itemscontrol, with a list of items (ParentCredentials). The problem is that these ParentCredentials are at the same level of the items being binded with the itemscontrol. Not sure if this is clear but if you have a look at the view model it should make more sense
这是我的viewmodel:
This is my viewmodel:
public class AccessControlViewModel : INotifyPropertyChanged
{
public ObservableCollection<LogonCredential> Credentials
{...}
public List<string> ParentCredentials
{...}
}
我有以下XAML 。
And i have the following XAML.
<ItemsControl ItemsSource="{Binding AccessControl.Credentials}" HorizontalContentAlignment="Stretch">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid >
<Grid.ColumnDefinitions >
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Content="{Binding Path=DisplayName}"/>
<ComboBox Grid.Column="2" ItemsSource="{Binding Source={RelativeSource AncestorType={x:Type vm:ResourceViewModel}}, Path=AccessControl.ParentCredentials}">
</ComboBox>
...
如何进行此绑定?还要注意,AccessControl是ResourceViewModel类的一部分。
How can I make this binding? Also note that AccessControl is a part of ResourceViewModel class.
推荐答案
您需要导航回到ItemsControl,并通过DataContext路径绑定。
You need to navigate back up to the ItemsControl, and bind via the DataContext path.
{Binding RelativeSource={RelativeSource AncestorType=ItemsControl}, Path=DataContext.AccessControl.ParentCredentials}
源= {RelativeSource ...
在任何情况下都不工作。此外,AncestorType总是一些FrameworkElement而不是一个数据对象。
Source={RelativeSource...
never works in any case. Further the AncestorType is always some FrameworkElement rather than a data-object.
这篇关于绑定项目控件中的组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!