从另一个用户控件单击按钮添加用户控件

时间:2014-03-10 22:14:30

标签: c# wpf

我有两个用户控件,当我点击第一个用户控件上的按钮时,另一个用户控件显示: 第一个用户控件是:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Navigateur.Presentation.UserControlWork
{
    /// <summary>
    /// Logique d'interaction pour ListeBlanche.xaml
    /// </summary>
    public partial class ListeBlanche : UserControl
    {
        public ListeBlanche()
        {
            InitializeComponent();
        }


        private async void Button_Click_1(object sender, RoutedEventArgs e)
        {
            ServiceReferenceParent.ParentServiceClient wcfParent = new ServiceReferenceParent.ParentServiceClient();
            bool existe = await wcfParent.LogInAsync(textmail.Text, textpass.Text);
            if (existe)
            {
                //grid.Children.Remove(this);
                //this.Visibility = System.Windows.Visibility.Collapsed;
                //ParentControl parmain = new ParentControl();
                //parmain.Visibility = System.Windows.Visibility.Visible;
                //parmain.
            }
            else
            {
                Popup1.IsOpen = true;

            }

        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {

            MainWindow main = new MainWindow();
            ParentControl parmain = new ParentControl();
            main.gridMain.Children.Add(parmain);
            parmain.Visibility = System.Windows.Visibility.Visible;

            this.Visibility = System.Windows.Visibility.Hidden;

        }


    }
}

当我点击“Button_Click_2”时,ParentControl会显示但它从不显示。

全屏: 这是ParenControl xaml代码:

`<UserControl x:Class="Navigateur.Presentation.UserControlWork.ParentControl"
             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" 
             mc:Ignorable="d" Width="586.194" Height="316.418">
    <Grid Background="#FF65B4EC" ClipToBounds="True">
        <Canvas HorizontalAlignment="Left" Height="69" VerticalAlignment="Top" Width="586" Background="#FF5ACAFF" ClipToBounds="True">
            <Label Content="Création de votre compte parent" Canvas.Left="188" Canvas.Top="20" RenderTransformOrigin="0.5,0.5" Width="155" Background="#FF65B4EC" FontWeight="Black">
                <Label.RenderTransform>
                    <TransformGroup>
                        <ScaleTransform/>
                        <SkewTransform/>
                        <RotateTransform Angle="-0.376"/>
                        <TranslateTransform/>
                    </TransformGroup>
                </Label.RenderTransform>
            </Label>
        </Canvas>
        <Label Content="Nom Compte" HorizontalAlignment="Left" Margin="121,106,0,0" VerticalAlignment="Top"/>
        <Label Content="Pseudo" HorizontalAlignment="Left" Margin="323,165,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.498,0.006"/>
        <Label Content="Prénom" HorizontalAlignment="Left" Margin="121,219,0,0" VerticalAlignment="Top"/>
        <Label Content="Nom" HorizontalAlignment="Left" Margin="323,214,0,0" VerticalAlignment="Top" RenderTransformOrigin="-0.2,0.471"/>
        <TextBox HorizontalAlignment="Left" Height="23" Margin="121,132,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="335"/>
        <TextBox HorizontalAlignment="Left" Height="23" Margin="323,191,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="133"/>
        <RadioButton Content="Monsieur" HorizontalAlignment="Left" Margin="121,191,0,0" VerticalAlignment="Top" Height="23"/>
        <RadioButton Content="Madame" HorizontalAlignment="Left" Margin="192,191,0,0" VerticalAlignment="Top" Height="23"/>
        <TextBox HorizontalAlignment="Left" Height="23" Margin="121,245,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="131"/>
        <TextBox HorizontalAlignment="Left" Height="23" Margin="323,245,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="133" RenderTransformOrigin="0.5,0.5"/>
        <Button Content="Annuler" HorizontalAlignment="Left" Margin="192,288,0,0" VerticalAlignment="Top" Width="75" Height="23"/>
        <Button Content="Suivant" HorizontalAlignment="Left" Margin="297,288,0,0" VerticalAlignment="Top" Width="75"/>

    </Grid>
</UserControl>`

这是我的主窗口xaml代码:

    <Window x:Name="wndmain" x:Class="Navigateur.Presentation.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:auth="clr-namespace:Navigateur.Presentation.UserControlWork"
        Title="MainWindow" Height="317" Width="586.194" WindowState="Maximized" WindowStyle="None" Topmost="True" ResizeMode="NoResize">
    <Grid x:Name="gridMain">
        <auth:ListeBlanche ClickedInUserControl="OnClickedInUserControl"></auth:ListeBlanche>
    </Grid>
</Window>

1 个答案:

答案 0 :(得分:2)

我认为您需要在UserControl中使用RoutedEvent才能使其正常工作。

因此,请将usercontrol中的click事件更改为:

public event RoutedEventHandler ClickedInUserControl;
private void Button_Click_2(object sender, RoutedEventArgs e)
{
        if (ClickedInUserControl != null)
        {
            ClickedInUserControl(this, new RoutedEventArgs());
        }         

}

然后在你的MainWindow的xaml中添加这个UserControl:

ClickedInUserControl="OnClickedInUserControl"

然后在主窗口的代码隐藏

上添加事件处理程序
private void OnClickedInUserControl(object sender, RoutedEventArgs e)
{
    ParentControl parmain = new ParentControl();
    this.gridMain.Children.Add(parmain);
    parmain.Visibility = System.Windows.Visibility.Visible;
    // also remove the original usercontrol from the grid and collapse it's vilisibilty
}

此外,我强烈建议您考虑使用命令而不是事件来进行此类操作,但我使用了事件,因为这是您正在使用的事件。