[英]How to pass information from one WPF UserControl to another 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.