2017-02-23 40 views
0

不幸的是,UserControl不顯示在我的工具箱中。 由於我已經工作,但與Windows窗體,我回來記得你也可以通過代碼創建此用戶控件。並且在Windows Form中進行了測試。WPF中的C#用戶控件

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?

編輯

enter image description here

如果我點擊按鈕 「Vorschau」 是與內容改變紅色區域。如果我點擊「Einstellung」按鈕,會出現與以前不同的內容。它應該改變,因爲內容總是隻在紅色區域,其餘的應該保持原樣。

+0

你應該使用'Grid'然後使用'Grid.Ro wDefinitions'以及'Grid.ColumnDefinitions'(在XAML編輯器中更容易看到)。在那裏對齊內容很簡單。然後,您也可以將「UserControl」添加到紅色部分,並在單擊「Vorschau」時僅切換「可見性」。 –

回答

0

在你主窗口你需要把一個ContentControl中將作爲佔位符和用戶控件的創建實例之後設置ContentControl中的內容:

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

,然後在代碼窗口的背後在按鈕內點擊evebt,你可以設置內容屬性使用新的用戶控件如下:

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

首先,感謝您的答案。我現在通過這個,但我點擊按鈕發生什麼。沒有內容顯示。 'private void Button_Click(object sender,RoutedEventArgs e) Einstellungcs settings = new Einstellungcs(); this.UserControlContainer.Content = settings; lbWhereIAm.Content =「Vorschau」; }' – GabelUndMesser

+0

用戶控件的外觀如何?並將ContentControl放置在窗口的主窗格中,那麼您是如何添加的? –

+0

'使用系統; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data;使用System.Linq的 ; using System.Text; using System.Threading.Tasks;使用System.Windows的 。形式; 命名空間Vorschau { 公共部分類Einstellungcs:用戶控件 { 公共Einstellungcs() { 的InitializeComponent(); this.Dock = DockStyle.Fill; } } }'http://www.bilder-upload.eu/show.php?file=205525-1487842565.png這就是它的樣子 – GabelUndMesser