简体   繁体   中英

c# XAML Access UserControl from other UserControl on same page

Similar question already answered here . The difference is that I have the following structure for my 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.

As I linked the other thread, I can't do the following:

I introduced a property at the top of Control1 code-behind:

 public Control2 ctrl2 { get; set; }

And then I gave the Control2 a name in the xaml file of the Page:

<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:

Control1.ctrl2 = NameOfControl;

With that doing I could access my UserControl2 from each insatance of UserControl1 but how do I do this?

Thanks

//IN RESPONSE TO THE COMMENT FROM AMINE:

this is the xaml of the Page:

        <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;

In the MainPage code-behind put:

this.DataContext = new YourViewModel();
Box.DataContext = this.DataContext;

And replace your xaml by this :

<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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM