C# 如何在 wpf 中显示用户对按钮单击的控制?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9240390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 06:30:44  来源:igfitidea点击:

How to show user control on button click in wpf?

c#wpfuser-controls

提问by Idrees Khan

I have created a user control in wpf. I have also create a mainwindow. Now, what i want is that, when i click on a button (which is in mainWindow), a user control is shown like a dialog box. suppose i have a button named i-e create new user. Now, what i want is to show the control (that i have created for new user) on button click whithout calling it in mainWindow.

我在 wpf 中创建了一个用户控件。我还创建了一个主窗口。现在,我想要的是,当我单击一个按钮(在 mainWindow 中)时,用户控件显示为一个对话框。假设我有一个名为 ie 创建新用户的按钮。现在,我想要的是在按钮单击时显示控件(我为新用户创建的),而无需在 mainWindow 中调用它。

采纳答案by NetSquirrel

In the WPF demo application Family Show, user controls are created in the main window (MainWindow.xaml)

在 WPF 演示应用程序Family Show 中,在主窗口 ( MainWindow.xaml)中创建了用户控件

<!-- New User Control -->
<local:NewUserControl x:Name="NewUserControl" 
HorizontalAlignment="Center" VerticalAlignment="Center"
AddButtonClick="NewUserControl_AddButtonClick"/>

Then in the code behind (MainWindow.xaml.cs), the different user controls are hidden or shown as a result of click actions from the button.

然后在后面的代码 ( MainWindow.xaml.cs) 中,不同的用户控件被隐藏或显示为按钮单击操作的结果。

/// <summary>
/// Hides the New User Control.
/// </summary>
private void HideNewUserControl()
{
     NewUserControl.Visibility = Visibility.Hidden;
     DiagramControl.Visibility = Visibility.Visible;
     enableButtons();

     if (family.Current != null)
        DetailsControl.DataContext = family.Current;
}

/// <summary>
/// Shows the New User Control.
/// </summary>
private void ShowNewUserControl()
{
        HideFamilyDataControl();
        HideDetailsPane();
        DiagramControl.Visibility = Visibility.Collapsed;
        WelcomeUserControl.Visibility = Visibility.Collapsed;

        if (PersonInfoControl.Visibility == Visibility.Visible)
            ((Storyboard)this.Resources["HidePersonInfo"]).Begin(this);

        NewUserControl.Visibility = Visibility.Visible;
        NewUserControl.ClearInputFields();
        NewUserControl.SetDefaultFocus();

        ... //Removed for brevity
    }

I encourage you to download the Family Show app to look at the source code, or to browseit at least on-line.

我鼓励您下载 Family Show 应用程序以查看源代码,或至少在线浏览它。

You could put it in a separate window like Johannes Hofmeister suggested with his answer.

你可以把它放在一个单独的窗口中,就像约翰内斯霍夫迈斯特在他的回答中建议的那样。

The main advantage of a User Control is that this user interface block might be used at multiple points in an application. (eg. a graph control user control with scroll, zoom and screenshot buttons would appear next to every graph, making it an ideal candidate for a user control).

用户控件的主要优点是该用户界面块可以在应用程序的多个点使用。(例如,带有滚动、缩放和屏幕截图按钮的图形控件用户控件将出现在每个图形旁边,使其成为用户控件的理想候选者)。

回答by cessor

You can easily add another window with the your usercontrol on it!

您可以轻松地添加另一个带有用户控件的窗口!

First, create another window (right click in the solution explorer, add new item, Window). Second, drag you usercontrol onto the window:

首先,创建另一个窗口(在解决方案资源管理器中右键单击,添加新项目,窗口)。其次,将您的用户控件拖到窗口上:

<Window x:Class="MyWpfApplication.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:DeleteMeTest="clr-namespace:DeleteMeTest"
        Title="Window1" Height="300" Width="300">
    <Grid>
        <MyWpfApplication:UserControl1 />
    </Grid>
</Window>

Then you must setup the button click handler to show the window:

然后您必须设置按钮单击处理程序以显示窗口:

MainWindow.xaml:

主窗口.xaml:

<Window x:Class="MyWpfApplication.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Content="Click Me" Click="Button_Click"/>
    </Grid>
</Window>

The Button_Click Handler in MainWindow.xaml.cs:

MainWindow.xaml.cs 中的 Button_Click 处理程序:

private void Button_Click(object sender, RoutedEventArgs e)
{
    new Window1().ShowDialog();
}

The ShowDialog() Method opens a dialog, which means that the window comes on top and must be interacted with (Closed) before you can return to interacting with your main window.

ShowDialog() 方法打开一个对话框,这意味着该窗口位于顶部并且必须与 (Closed) 进行交互,然后才能返回与主窗口的交互。

You can also use the Show Method, to have a non blocking window.

您还可以使用 Show 方法来获得非阻塞窗口。