【问题标题】:How to Pass a Value From a Window to a UserControl in WPF如何将值从窗口传递到 WPF 中的用户控件
【发布时间】:2016-07-01 23:35:40
【问题描述】:

我想将一个值从 MainWindow 传递到我的 UserControl!我向我的 UserControl 传递了一个值,并且 UserControl 向我显示了 MessageBox 中的值,但它没有显示 TextBox 中的值。这是我的代码:

MainWindow(将值传递给 UserControl)

try
{
    GroupsItems abc = null;
    if (abc == null)
    {
        abc = new GroupsItems();
        abc.MyParent = this;
        abc.passedv(e.ToString(), this);

    }
}
catch (Exception ee)
{
    MessageBox.Show(ee.Message);
}

用户控制

public partial class GroupsItems : UserControl
{
    public MainWindow MyParent { get; set; }
    string idd = "";
    public GroupsItems()
    {
        InitializeComponent();
        data();
    }

    public void passedv(string id, MainWindow mp)
    {
        idd = id.ToString();
        MessageBox.Show(idd);
        data();
    }

    public void data()
    {
        if (idd!="")
        {
            MessageBox.Show(idd);
            texbox.Text = idd;
        }
    }
}

编辑(使用 BINDING 和 INotifyProperty )

.....

   public GroupsItems()
      {
            InitializeComponent();
      }

    public void passedv()
    {
        textbox1.Text = Text;
    }

}

public class Groupitm : INotifyPropertyChanged
{

    private string _text = "";

    public string Text
    {
        get { return _text; }
        set
        {
            if (value != _text)
            {
                _text = value;
                NotifyPropertyChanged();
            }
        }
    }



    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged(String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

【问题讨论】:

  • 老兄...您正在尝试像使用 WinForms 一样使用 WPF。它只是不能那样工作。在编写更多 WPF 之前,请先了解如何使用数据 BindingINotifyPropertyChanged interface
  • @Sheridan 如果他不想,我认为他不必使用Binding、MVVM 等。我同意你的观点,这是终极体验,但仍然可以使用旧模式。
  • @Sheridan 我使用绑定的代码也不起作用:(
  • @meilke,我完全接受一切皆有可能。请注意,我没有提到 MVVM 或终极体验。只是问题作者如果用 WPF 方式而不是他当前使用的 WinForms 方式编写 WPF,会发现 WPF 更容易编写。
  • @Sheridan 请花点时间查看我更新的代码

标签: c# wpf user-controls


【解决方案1】:

这里的问题是有参考的。

当您在后面的代码中创建新对象时,将创建新对象,这与您在 xaml 代码中拥有的对象不同。所以你应该使用以下代码:

<local:GroupsItems x:Name="myGroupsItems"/>

并且在后面的代码中您不必创建新对象。您应该使用在 XAML 中添加的对象:

...
myGroupsItems.MyParent = this;
myGroupsItems.passedv(e.ToString(), this);
...

Here 是示例解决方案 (sampleproject)。

【讨论】:

  • 谢谢kmatyaszek!保持祝福:)
  • 名称不允许数字,所以我改用&lt;local:GroupsItems x:Uid="1"/&gt;
【解决方案2】:

idd 仍然是"" 时,您在构造函数中调用data,这导致文本框仍然为空。更改 MyParent 属性不会改变这一点。只有passedv 可以。但是此时您没有父集。也可以在passedv 中拨打data

【讨论】:

  • 你收到MessageBox.Show(idd)data的第二条消息了吗
  • @Bharath 是的,我收到了这条消息
  • 嗯...如果您收到第二条消息,文本框必须由 idd 文本填充。尝试清除并重建您的解决方案。
【解决方案3】:

试试这个:

public partial class GroupsItems : UserControl
{
   //properties and methods

    private string idd="";

    public string IDD
    {
    get{return idd;}
    set{
        idd=value; 
        textBox1.Text=idd;
        }
    }

   //other properties and methods
}

用法:

在您的主窗体中:

    abc = new GroupsItems();
    abc.IDD="sometext";
    MainGrid1.Children.Add(abc);   //Grid or any other container for your UserControl

【讨论】:

    【解决方案4】:

    在您的 Binding 示例中,您的 GroupItem 类看起来没问题,只是您需要传入更改后的属性的名称:

        public string Text
        {
            get { return _text; }
            set
            {
                if (value != _text)
                {
                    _text = value;
                    NotifyPropertyChanged("Text");
                }
            }
        }
    

    现在,在GroupsItems 中,您不应该访问TextBox。在 WPF 中,我们操作的是数据,而不是 UI……但是当我们使用 Binding 对象将数据绑定到 UI 控件时,它们会自动更新(如果我们正确实现了 INotifyPropertyChanged 接口 em>)。

    首先,让我们像在 GroupItem 类中所做的那样,在后面的代码中添加一个数据属性(它也应该实现 INotifyPropertyChanged 接口):

    private GroupItem _item = new GroupItem();
    
    public GroupItem Item
    {
        get { return _item; }
        set
        {
            if (value != _item)
            {
                _item = value;
                NotifyPropertyChanged("Item");
            }
        }
    }
    

    现在让我们尝试在TextBox.Text 属性上使用Binding

    <TextBox Text="{Binding Item.Text}" />
    

    看看我们如何将GroupItem 类的Text 属性绑定到TextBox.Text 属性...现在我们需要做的就是更改Item.Text 属性的值并观察它在用户界面:

    <Button Content="Click me" Click="Button_Click" />
    
    ...
    
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Item.Text = "Can you see me now?";
    }
    

    或者,如果您在项目的其他地方调用该代码,则可以将此代码放入您的 passedv 方法中。让我知道你的进展情况。


    更新>>>

    在您的GroupItem 类中,尝试将初始化更改为:

    private string _text = "Any text value";
    

    您现在可以在运行应用程序时在 UI 中看到该文本吗?如果没有,请尝试将整个 Text 属性添加/复制到您的代码中,并将 TextBox 声明更改为:

    <TextBox Text="{Binding Text}" />
    

    如果你现在看不到文本值,你真的有问题......你在你的代码中实现了INotifyPropertyChanged 接口,不是吗?

    【讨论】:

      猜你喜欢
      • 2021-11-12
      • 1970-01-01
      • 2011-01-27
      • 2017-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-23
      相关资源
      最近更新 更多