问题描述
我的WPF表单正在使用多个用户控件。这些代表独特的形式。我需要提供某种导航功能。
My WPF form is using multiple user controls. These represents unique forms. I need to give navigational kind of functionality.
一旦用户单击UserControl A上的EDIT按钮,我想绑定UserControl B的DataContext并打开此用户控件
Once a user clicks on EDIT button which is on UserControl A, I want to bind DataContext of UserControl B and Open this user control on master form.
此主表单同时包含用户控件。我该怎么办?
This master form contains both the user control. How can I do that?
这是我的代码的样子
<Window>
<TabControl Background="Transparent">
<TabItem Header="View Registration" Background="Transparent">
<my:BulkPersonRegistration x:Name="BulkPersonRegistrationForm" />
</TabItem>
<TabItem Header="Add/Update Person" Background="Transparent">
<my:PersonManager x:Name="PersonManagerForm" />
</TabItem>
</TabControl>
</Window>
BulkPersonRegistration用户控件调用数据绑定函数以显示数据网格。绑定数据网格后,每行之后会出现一个编辑按钮。当用户单击编辑按钮时,我希望PersonManager表单被编辑行的对象绑定。
问题是BulkPersonRegistration表单不知道PersonManagerForm。因此,我需要一种方法来找出BulkPersonRegistration的Parent表单,即Owner表单,然后找到Personmanager表单,最后设置它的数据上下文。我认为所有这些都需要在BulkRegistration表单上完成。我宁愿在Window而不是用户控件上进行此操作。
BulkPersonRegistration user control calls a function for data binding to display a datagrid. Upon binding of a datagrid, edit button appears after every row. When user clicks edit button, I want PersonManager form to be binded by the object of edited row.Problem is, BulkPersonRegistration form is not aware of PersonManagerForm. So I need a way to find out Parent form of BulkPersonRegistration i.e. Owner form and then find Personmanager form and finally setting it's data context. I think all of this needs to be done on BulkRegistration form. I would prefer doing this on Window rather than user control.
请提出建议。
推荐答案
您可以通过使用 ElementName 这样的绑定简单地实现此目的
Hi You can achieve this simply by Binding using ElementName like this
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<local:UserControl1 x:Name="uc1"/>
<local:UserControl2 Grid.Row="1" DataContext="{Binding DataContext, ElementName=uc1}"/>
</Grid>
我希望这会有所帮助。
这篇关于需要从另一个UserControl设置一个UserControl的DataContext,这两个控件都在同一WPF页面上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!