wpf c# XAML 从同一页面上的其他 UserControl 访问 UserControl
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36716149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c# XAML Access UserControl from other UserControl on same page
提问by Ulpin
Similar question already answered here. The difference is that I have the following structure for my Page-UserControl1-UserControl2:
类似的问题已经在这里回答。不同之处在于我的 Page-UserControl1-UserControl2 具有以下结构:
On the Page I have a ListBox with several ListBoxItems whith each Item is based on the UserControl1. The UserControl is also based on the same Page but there is only one instance of it.
在页面上,我有一个带有多个 ListBoxItems 的 ListBox,每个项目都基于 UserControl1。UserControl 也基于相同的 Page,但它只有一个实例。
As I linked the other thread, I can't do the following:
当我链接另一个线程时,我无法执行以下操作:
I introduced a property at the top of Control1 code-behind:
我在 Control1 代码隐藏的顶部引入了一个属性:
public Control2 ctrl2 { get; set; }
And then I gave the Control2 a name in the xaml file of the Page:
然后我在页面的 xaml 文件中给 Control2 一个名字:
<local:Control2 Grid.Row="2" x:Name="NameOfControl2"></local:Control2>
The next step I would like to do is to pass the instance of Control2 to the property just set in Control1. So in the OnLoad Method in the code-behind of the Page I want to do the following which cant work because the instance of Control1 is unknown at this point:
我想做的下一步是将 Control2 的实例传递给刚刚在 Control1 中设置的属性。因此,在页面代码隐藏的 OnLoad 方法中,我想执行以下操作,因为此时 Control1 的实例未知:
Control1.ctrl2 = NameOfControl;
With that doing I could access my UserControl2 from each insatance of UserControl1 but how do I do this?
这样做后,我可以从 UserControl1 的每个实例访问我的 UserControl2,但我该怎么做?
Thanks
谢谢
//IN RESPONSE TO THE COMMENT FROM AMINE:
//回应 Amine 的评论:
this is the xaml of the Page:
这是页面的xaml:
<ListView Grid.Row="1" Margin="0 0 0 5"
x:Name = "Box"
HorizontalAlignment="Center"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ContinuumNavigationTransitionInfo.IsEntranceElement="True">
<ListView.ItemTemplate>
<DataTemplate>
<local:UserControl1 x:Name="NameOfControl1" DataContext="{Binding ElementName=Box,Path=DataContext}"/>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel VerticalAlignment="Bottom"/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
And in the code-behind I fill the Items with:
在代码隐藏中,我用以下内容填充项目:
StandardTweetBox.Items?.Add(MySpecialClass);
//this.DataContext = NameOfSendControl;
StandardTweetBox.DataContext = NameOfSendControl;
回答by Amine
In the MainPage code-behind put:
在 MainPage 代码隐藏中:
this.DataContext = new YourViewModel();
Box.DataContext = this.DataContext;
And replace your xaml by this :
并用这个替换你的xaml:
<ListView Grid.Row="1" Margin="0 0 0 5"
x:Name = "Box"
HorizontalAlignment="Center"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ContinuumNavigationTransitionInfo.IsEntranceElement="True">
<ListView.ItemTemplate>
<DataTemplate>
<local:UserControl1 x:Name="ctrl1name" DataContext="{Binding ElementName=Box,Path=DataContext}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
And
和
<local:Control2 Grid.Row="2" x:Name="NameOfControl2" DataContext="{Binding ElementName=Box,Path=DataContext}"></local:Control2>
Now, the mainpage,usercontrl1 and usercontrl2 will have the same ViewModel
现在,主页、usercontrl1 和 usercontrl2 将具有相同的 ViewModel

