使用 ICommand C# 将 UIElement 添加到 MainWindow

问题描述

我想通过 ICommand 动态添加 UIElement。 它与 EventHandler 一起工作正常,但是当我尝试使用 ICommand 做同样的事情时,UIElement 是不可见的,但是它接缝堆栈面板包含它,因为添加后“容量”和“计数”为 1。

ICommand (AddElement) 可以在 viewmodel 中找到,EventHandler 可以在 MainWindow 中找到。

我做了一个比较。你能检查一下还缺少什么吗?如果提供的信息不够,请告诉我。 非常感谢提前!

public void AddElement(StackPanel sp)
    {
        Button btn = new Button()
        {
            Content = "TestButton",Height = 20,Width = 20,};
       sp.Children.Add(btn);
    }

public void AddElem(object msg)
    {
        MainWindow mw = new MainWindow();
        mw.Owner = System.Windows.Application.Current.MainWindow;
        _actions.AddElement(mw.TestStack);
    }


private void BtnAddList_Click(object sender,RoutedEventArgs e)
    {
        Button btn = new Button()
        {
            Content = "TestButton",};
        TestStack.Children.Add(btn);
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)