WPF,Caliburn.micro:如何在当前绑定上下文之外绑定 属性?
WPF, Caliburn.micro : How to bind property outside of current binding context?
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Key}"></TextBlock>
<ComboBox ItemsSource="{Binding Value}" SelectedItem="{Binging SomeProperty}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"></TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</StackPanel>
我在 Dictionary 中,我想绑定 SelectedItem="{Binging SomeProperty}"
SomeProperty 是 ViweModel 的 属性,不在 Dictionary 中。怎么做?如何绑定到当前绑定上下文之外的属性。
如果 Binding
到 Window.DataContext
的 属性,您可以使用 RelativeSource
进行以下操作:
<ComboBox ItemsSource="{Binding Value}" SelectedItem="{Binding DataContext.SomeProperty, RelativeSource={RelativeSource AncestorType=Window}}">
或者如果您想在某些 TextBox
中显示 SelectedItem
(例如 string
的集合):
<TextBox Text="{Binding MyCollection/}"/>
<ComboBox ItemsSource="{Binding MyCollection}" IsSynchronizedWithCurrentItem="True">
MyCollection
with "/" 获取用作 MyCollection
的 DefaultView
的 ICollectionView
的当前项目。 Read more >>>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Key}"></TextBlock>
<ComboBox ItemsSource="{Binding Value}" SelectedItem="{Binging SomeProperty}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"></TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</StackPanel>
我在 Dictionary 中,我想绑定 SelectedItem="{Binging SomeProperty}"
SomeProperty 是 ViweModel 的 属性,不在 Dictionary 中。怎么做?如何绑定到当前绑定上下文之外的属性。
如果 Binding
到 Window.DataContext
的 属性,您可以使用 RelativeSource
进行以下操作:
<ComboBox ItemsSource="{Binding Value}" SelectedItem="{Binding DataContext.SomeProperty, RelativeSource={RelativeSource AncestorType=Window}}">
或者如果您想在某些 TextBox
中显示 SelectedItem
(例如 string
的集合):
<TextBox Text="{Binding MyCollection/}"/>
<ComboBox ItemsSource="{Binding MyCollection}" IsSynchronizedWithCurrentItem="True">
MyCollection
with "/" 获取用作 MyCollection
的 DefaultView
的 ICollectionView
的当前项目。 Read more >>>