0

我的项目主窗口中有两个用户控件。在MainWindow我调用内容为 a 的第一个 UserControlButton并将这个 FirstControl 放入 Grid 中。FirstUserControl当我单击MainWindow中的按钮时,如何调用第二个 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>
4

1 回答 1

1

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

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

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

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

于 2013-07-28T23:19:19.857 回答