【问题标题】:C# UserControl in WPFWPF 中的 C# 用户控件
【发布时间】:2017-02-23 17:28:49
【问题描述】:

不幸的是,UserControl 没有显示在我的工具箱中。 由于我使用过 Windows 窗体,因此我记得您也可以通过代码创建此 UserControl。并且还在 Windows 窗体中进行了测试。

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 );
}

这些代码 sn-ps 有时应该能说明我的意思。 不幸的是,WPF 中不再有面板。 那么我怎样才能在 WPF 中做到这一点呢?我目前使用 Canvas,但这不是正确的。有人将如何获得我必须使用的解决方案或我必须如何制作它?

我想通过单击按钮更改内容区域 使用 UserControl。有人可以向我解释如何在 WPF 中使用 UserControl 吗?

编辑

如果我点击按钮“Vorschau”是用内容改变红色区域。如果我单击“Einstellung”按钮,则会出现与以前不同的内容。它应该改变,因为内容总是只在红色区域,其余的应该保持原样。

【问题讨论】:

  • 您应该使用Grid,然后使用Grid.RowDefinitionsGrid.ColumnDefinitions(在XAML 编辑器中更容易看到)。在那里对齐内容很简单。然后,您还可以将 UserControl 添加到红色部分,并且仅在单击“Vorschau”时切换 Visibility

标签: c# wpf user-controls


【解决方案1】:

在您的 MainWindow 中,您需要放置一个 ContentControl 作为占位符,并在创建 usercontrol 实例后设置 contentcontrol 的内容:

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

然后在 Window 后面的代码里面点击按钮点击 evebt,你可以设置 content 属性来使用新的用户控件,方法如下:

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

【讨论】:

  • 首先,感谢您的回答。我现在已经采用了这个,但是我点击按钮没有任何反应。不显示任何内容。 private void Button_Click(object sender, RoutedEventArgs e) { Einstellungcs settings = new Einstellungcs(); this.UserControlContainer.Content = settings; lbWhereIAm.Content = "Vorschau"; }
  • 你的用户控件是什么样子的?并将 ContentControl 放在 Window 的主网格中,您是如何添加的?
  • using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Vorschau { public partial class Einstellungcs : UserControl { public Einstellungcs() { InitializeComponent(); this.Dock = DockStyle.Fill; } } } bilder-upload.eu/show.php?file=205525-1487842565.png 是这样的
  • 在里面放一个文本框,看看它是否出现,如果可行,然后尝试从后面的代码中分配 UserControl
猜你喜欢
  • 1970-01-01
  • 2011-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-08
  • 2021-11-26
  • 1970-01-01
  • 2010-10-01
相关资源
最近更新 更多