如何从 WPF 中同一窗口中的另一个用户控件调用?

问题描述 投票:0回答:2

我的项目的主窗口中有两个用户控件。在

MainWindow
中,我调用第一个内容为
Button
的 UserControl,并将此 FirstControl 放入网格中。当我点击主窗口
FirstUserControl
中的按钮时,如何调用第二个UserControl?
第一个用户控件:

<UserControl x:Class="BenashManage.UserControl.ButtonUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             Height="auto" Width="auto" RenderTransformOrigin="0,0">
    <Grid>
        <Border x:Name="BorderAddEdit"  BorderBrush="{DynamicResource BorderBrush}" BorderThickness="5,5,5,5" CornerRadius="9,9,9,9" Background="{x:Null}">
            <Grid Margin="0.2,0.2,5.4,4.2">
                <Grid.RowDefinitions>
                    <RowDefinition Height="3*"/>
                </Grid.RowDefinitions>
                <Button Content="one"  TextBlock.Foreground="White"  Grid.Row="1"  Margin="9,9,9,9"                  Height="28" TextBlock.FontSize="15" Name="btn_MartyMang" Click="click_Marty"/>
            </Grid>
        </Border>
    </Grid>
</UserControl>

第二个用户控件:

<UserControl x:Class="BenashManage.UserControl.InjuredUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             Height="auto" Width="auto"
             mc:Ignorable="d" RenderTransformOrigin="0,0" FontFamily="Arial" FontSize="14"   TextBlock.Foreground="White">

    <Grid VerticalAlignment="Top" Height="auto" Width="360"  HorizontalAlignment="Right">
        <Border x:Name="BorderAddEdit"  Margin="6,2,6,6"  BorderThickness="5,5,5,5" CornerRadius="9,9,9,9" Background="{x:Null}">
            <Grid VerticalAlignment="Center" >
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto" MinHeight="20.8"/>
                </Grid.ColumnDefinitions>
                <TextBlock TextWrapping="Wrap" Text=":jjj" Grid.Column="1" Grid.Row="0" VerticalAlignment="Center" HorizontalAlignment="Right" Margin="0,4,-0.4,3"/>
                <TextBox  Grid.Row="6" TextAlignment="Right" VerticalAlignment="Center" Margin="3,2.8,2.2,2.2" />

            </Grid>
        </Border>

    </Grid>

</UserControl>

主窗口:

<Window x:Class="BenashManage.MartyrManage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:controls="clr-namespace:BenashManage.UserControl"
        Title="MartyrManage" Height="550" Width="550"  Style="{DynamicResource ModalWindowStyle}" Loaded="Window_Loaded_1">
    <Window.CommandBindings>
        <CommandBinding Command="Close"
        Executed="CloseCommand_Executed"/>
    </Window.CommandBindings>
    <Grid >
        <Grid Margin="179,10,10.4,10" HorizontalAlignment="Center" VerticalAlignment="Center" 
*******
Height="456" Width="357" Name="MoveToUserControl" ></Grid>
        <Grid Margin="10,71,372.4,67" HorizontalAlignment="Center" VerticalAlignment="Center" Height="338" Width="auto" Name="ButtonManage" >
            <controls:FirstUserControl Margin="0,0,0,92">

            </controls:FirstUserControl>
        </Grid>
    </Grid>
</Window>
wpf c#-4.0 user-controls
2个回答
1
投票

有几种方法可以做到这一点:

1) 获取 UserControl 的父级,然后获取其子级。

(((control1).Parent as Panel).Children[1] as UserControl)

2) 在一个 UserControl 中引发一个由 MainWindow 处理的事件,以调用另一个 UserControl 中的函数。


0
投票

这对我有帮助: 在 FirstUserControl 事件 Button_Click 中:

var b = Window.GetWindow(this) as MainWindow;
b.SecondUserControl.DoWhatYouNeed...;
© www.soinside.com 2019 - 2024. All rights reserved.