从另一个用户控件单击按钮时添加用户控件
本文关键字:用户 控件 添加 按钮 单击 另一个 | 更新日期: 2023-09-27 17:49:34
我有两个用户控件,当我点击第一个用户控件的按钮时,另一个用户控件显示:第一个用户控件是:
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>
我认为你需要在你的UserControl中使用一个RoutedEvent来让这个工作。
所以把你的usercontrol中的click事件改成:
public event RoutedEventHandler ClickedInUserControl;
private void Button_Click_2(object sender, RoutedEventArgs e)
{
if (ClickedInUserControl != null)
{
ClickedInUserControl(this, new RoutedEventArgs());
}
}
然后在主窗口的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
}
另外,我强烈建议您使用命令而不是事件来处理这类事情,但我使用事件是因为您正在使用事件。