2017-02-23 48 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