簡體   English   中英

WPF中的C#UserControl

[英]C# UserControl in WPF

不幸的是,UserControl沒有顯示在我的工具箱中。 由於我曾經使用過Windows Form,因此我回想起您還可以通過代碼創建此UserControl。 並且還可以在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 );
}

這些代碼片段有時應說明我的意思。 不幸的是,WPF中沒有更多的面板。 那么如何在WPF中做到這一點? 我目前使用的是Canvas,但這不是正確的選擇。 有人將如何解決我必須使用的解決方案,或者我必須如何解決?

我想通過單擊帶有UserControl的按鈕來更改內容區域。有人可以向我解釋如何在WPF中使用UserControl嗎?

編輯

在此處輸入圖片說明

如果我單擊按鈕“ Vorschau”,則將內容更改為紅色區域。 如果單擊按鈕“ Einstellung”,則會顯示與以前不同的內容。 它的內容應始終僅在紅色區域中更改,其余部分應保持原樣。

在您的MainWindow中,您需要放置一個ContentControl作為占位符,並在創建usercontrol實例后設置contentcontrol的內容:

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

然后在窗口內部的代碼內,單擊evebt,可以按以下方式將content屬性設置為使用新的用戶控件:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM