Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在按钮上添加用户控件从另一个用户控件单击_C#_Wpf - Fatal编程技术网

C# 在按钮上添加用户控件从另一个用户控件单击

C# 在按钮上添加用户控件从另一个用户控件单击,c#,wpf,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.Contr

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

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;

        }


    }
}
使用系统;
使用System.Collections.Generic;
使用System.Collections.ObjectModel;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
命名空间Navigateur.Presentation.UserControlWork
{
/// 
///Logique d'interaction pour listblanche.xaml
/// 
公共部分类ListBlanche:UserControl
{
公众李斯特
{
初始化组件();
}
专用异步无效按钮\u单击\u 1(对象发送方,路由目标)
{
ServiceReferenceParent.ParentServiceClient wcfParent=新的ServiceReferenceParent.ParentServiceClient();
bool existe=wait wcfParent.LogInAsync(textmail.Text,textpass.Text);
如果(存在)
{
//grid.Children.Remove(这个);
//this.Visibility=System.Windows.Visibility.Collapsed;
//ParentControl parmain=新的ParentControl();
//parmain.Visibility=System.Windows.Visibility.Visible;
//帕尔曼。
}
其他的
{
Popup1.IsOpen=真;
}
}
私有无效按钮\u单击\u 2(对象发送方,路由目标)
{
MainWindow main=新的MainWindow();
ParentControl parmain=新的ParentControl();
main.gridMain.Children.Add(parmain);
parmain.Visibility=System.Windows.Visibility.Visible;
this.Visibility=System.Windows.Visibility.Hidden;
}
}
}
当我点击“Button\u click\u 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>`
    <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>
`
`
这是我的主窗口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>`
    <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>

我认为您需要在UserControl中使用RoutedEvent来实现此功能

因此,将usercontrol中的单击事件更改为:

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"
然后在主窗口的codebehind上添加事件处理程序

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
}

另外,我强烈建议您考虑使用命令而不是事件来处理这类事情,但我使用事件是因为您正在使用事件。

我猜
main
MainWindow
的一个新实例,但不是用户控件的父实例?谢谢@safetyOtter,它工作,但我需要我的用户控制,以全屏显示。我该怎么做???您还可以详细说明有关使用命令的更多信息(给我教程…)再次感谢您。很高兴提供帮助!“全屏”是指您希望控件填充其容器还是希望应用程序填充您的windows屏幕?不管怎样,我都需要更多地了解你的xaml。至于命令,这是我开始学习的教程,但后来我转到了MVVM Light框架,用于新项目,所谓全屏,我的意思是我的主窗口处于全屏模式,我需要我的用户控制来完成它。再次感谢您的合作。这个网站对我来说就像一个天堂。。。