繁体   English   中英

如何将值从窗口传递到WPF中的UserControl

[英]How to Pass a Value From a Window to a UserControl in WPF

我想将值从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));
        }
    }

这里的问题是参考。

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

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

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

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

是示例解决方案(sampleproject)。

idd仍为""时,您正在构造函数中调用data ,这导致文本框仍然为空。 更改MyParent属性不会更改该设置。 只有passedv可以。 但是到那时您还没有父集。 只需在passedv调用data passedv

尝试这个:

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

Binding示例中, GroupItem类看起来正常,除了需要传递更改后的属性的名称之外:

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

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

因此,首先,像在GroupItem类中一样,在后面的代码中添加一个data属性(它也应该实现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属性的值并在UI中观看它的更新:

<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接口,不是吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM