How to Pass a Value From a Window to a UserControl in WPF

user2835256

I want to pass a value from MainWindow into my UserControl! I passed a value to my UserControl and the UserControl showed me the value in a MessageBox, but it is not showing the value in a TextBox. Here is my code:

MainWindow(Passing Value To 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);
}

UserControl

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

EDIT(using BINDING and 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));
        }
    }
kmatyaszek

Problem here is with reference.

When you create new object in code behind, new object will be created and this is not the same object which you have in xaml code. So you should use following code:

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

and in code behind you don't have to create new object. You should use object that you added in XAML:

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

Here is example solution (sampleproject).

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

How to return value from child window to parent window using window.open in Javascript?

来自分类Dev

WPF UserControl不显示

来自分类Dev

带ContextMenu wpf的UserControl

来自分类Dev

WPF的UserControl多个DataContexts?

来自分类Dev

从窗体UserControl转换为WPF UserControl

来自分类Dev

WPF视图-UserControl路径,而不是UserControl内容

来自分类Dev

WPF是否真的需要UserControl?

来自分类Dev

来自UserControl的WPF数据绑定

来自分类Dev

WPF真的需要UserControl吗?

来自分类Dev

WPF UserControl上的TwoWay绑定

来自分类Dev

WPF + Unity:使用服务的UserControl?

来自分类Dev

WPF UserControl xaml的标记无效

来自分类Dev

从 UserControl 获取 WPF TabItem 容器

来自分类Dev

WPF UserControl 通用单击事件

来自分类Dev

将WPF窗口投射到WPF UserControl

来自分类Dev

将WPF窗口投射到WPF UserControl

来自分类Dev

WPF UserControl继承另一个UserControl

来自分类Dev

父 UserControl WPF 中的子 UserControl TextBox 值

来自分类Dev

如何将 WPF UserControl 留在 UserControl 内的某处?

来自分类Dev

如何从UserControl的外部Window继承资源?

来自分类Dev

将UserControl属性绑定到Window属性

来自分类Dev

WPF-从UserControl ViewModel引发事件

来自分类Dev

背景颜色阻碍WPF UserControl内容

来自分类Dev

不接受UserControl中的WPF多重绑定

来自分类Dev

在ListView数据模板WPF中绑定UserControl

来自分类Dev

WPF UserControl.Loaded事件不会触发

来自分类Dev

在WPF中将TabItem投射到UserControl

来自分类Dev

在WPF中设置子UserControl的datacontext

来自分类Dev

在UserControl WPF的视口中使用VisualBrush