将来自不同来源的数据绑定到ItemsControl中的ComboBox

时间:2019-02-21 22:57:02

标签: wpf binding

我有一个ItemsControl,其中的ItemsSource来自我的ViewModel。这可以按预期工作。

我在ItemsControl的DataTemplate中有一个组合框,看起来像这样...

<ComboBox SelectedItem="{Binding Path=RelayType}" ItemsSource="{Binding Path=RelayTypes" />

RelayTypes是ViewModel中的一个属性,但是我不知道如何在ItemsControl中用它来填充ComboBox。

我尝试了以下方法,但是没有运气,我不确定还有什么方法可以尝试...

<ComboBox SelectedItem="{Binding Path=RelayType}" ItemsSource="{Binding Path=DataContext.RelayTypes" />

<ComboBox SelectedItem="{Binding Path=RelayType}" ItemsSource="{Binding Path=RelayTypes, Source=DataContext" />

有什么想法吗?

编辑:

这有效:

{Binding Path=DataContext.RelayTypes, RelativeSource={RelativeSource AncestorType=local:MainView}}

0 个答案:

没有答案