简体   繁体   English

WPF中的C#UserControl

[英]C# UserControl in WPF

Unfortunately, the UserControl is not displayed in my ToolBox. 不幸的是,UserControl没有显示在我的工具箱中。 Since I have worked but with Windows Form, I have back remembers that you can create this UserControl also by code. 由于我曾经使用过Windows Form,因此我回想起您还可以通过代码创建此UserControl。 And also tested in Windows Form where it has worked. 并且还可以在Windows Form上进行测试。

public partial class Einstellungcs : UserControl
{
    public Einstellungcs()
    {
        InitializeComponent();
        this.Dock = DockStyle.Fill;
    }
}

public partial class MainWindow : Window
{
    Einstellungcs settings = new Einstellungcs();
    public MainWindow()
    {
        InitializeComponent();
    }
            ......
}

private void Button_Click(object sender, RoutedEventArgs e)
{
    panel.ContentMenu(settings );
}

These Code snippets Should sometimes illustrate what I mean. 这些代码片段有时应说明我的意思。 Unfortunately, there is no more panel in WPF. 不幸的是,WPF中没有更多的面板。 So how can I do this in WPF? 那么如何在WPF中做到这一点? I currently use Canvas, but this is not the right one. 我目前使用的是Canvas,但这不是正确的选择。 How would someone have the solution what I must use or how I have to make it? 有人将如何解决我必须使用的解决方案,或者我必须如何解决?

I would like to change the content area by clicking the button with a UserControl.Can someone explain to me how I can use the UserControl in WPF? 我想通过单击带有UserControl的按钮来更改内容区域。有人可以向我解释如何在WPF中使用UserControl吗?

EDIT 编辑

在此处输入图片说明

If I click on the button "Vorschau" is to change the red area with a content. 如果我单击按钮“ Vorschau”,则将内容更改为红色区域。 If I click on the button "Einstellung" a different content appears than before. 如果单击按钮“ Einstellung”,则会显示与以前不同的内容。 It should change as the content always only in the red area, the rest should remain as it is. 它的内容应始终仅在红色区域中更改,其余部分应保持原样。

In your MainWindow you need to put a ContentControl which will act as placeholder and after creating instance of the usercontrol set the content of the contentcontrol : 在您的MainWindow中,您需要放置一个ContentControl作为占位符,并在创建usercontrol实例后设置contentcontrol的内容:

 <ContentControl x:Name="UserControlContainer"></ContentControl>

and then in code behind of Window inside the button click evebt, you can set the content property to use new user control following way: 然后在窗口内部的代码内,单击evebt,可以按以下方式将content属性设置为使用新的用户控件:

Einstellungcs settings = new Einstellungcs();
this.UserControlContainer.Content = settings ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM