绑定到祖先的Datacontext [英] Binding to a Datacontext of an ancestor

查看:124
本文介绍了绑定到祖先的Datacontext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最好先显示你的代码,然后提出一个问题:

It would be best if showed you my code first and then ask the question:

<Window  DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Grid>
<DataGrid ItemsSource="{Binding Printers}" >
<DataGrid.Columns>
<DataGridComboBoxColumn Header="Drucker Typ" ItemsSource="{Binding Relative" SelectedItemBinding="{Binding PrinterType, Mode=TwoWay}"  Width="Auto" ItemsSource="{Binding}" />
</DataGrid.Columns>
</DataGrid>
</Window>

我有 DataGridComboBoxColumn 并且要绑定 ItemsSource 到窗口的 DataContext SelectedItem 到目前的 ItemsSource DataGrid的对象。

I have the DataGridComboBoxColumn and want to bind the ItemsSource to the DataContext of the Window and the SelectedItem to the current ItemsSource Object of the DataGrid.

如何做?

谢谢!

推荐答案

为避免您必须清楚NameScope的ElementName,您还可以使用FindAncestor。您可以在此定义要绑定到的父对象的类型。

To avoid the ElementName where you have to be well aware of the NameScope you can also use FindAncestor. Here you define the Type of the parent object you want to bind to.

示例:

<TextBlock Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=Text}" />

这篇关于绑定到祖先的Datacontext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆