如何在 WPF 中将值从窗口传递到用户控件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19514617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Pass a Value From a Window to a UserControl in WPF
提问by 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 传递到我的 UserControl 中!我向我的 UserControl 传递了一个值,UserControl 向我显示了 MessageBox 中的值,但它没有显示 TextBox 中的值。这是我的代码:
MainWindow(Passing Value To UserControl)
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);
}
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 )
编辑(使用 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));
}
}
回答by 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:
当您在后面的代码中创建新对象时,将创建新对象,这与您在 xaml 代码中拥有的对象不同。所以你应该使用以下代码:
<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:
在后面的代码中,您不必创建新对象。您应该使用您在 XAML 中添加的对象:
...
myGroupsItems.MyParent = this;
myGroupsItems.passedv(e.ToString(), this);
...
Hereis example solution (sampleproject).
这是示例解决方案(sampleproject)。
回答by meilke
You are calling datain the constructor when iddis still ""which results in the text box still being empty. Changing the MyParentproperty does not change that. Only passedvdoes. But at that point you do not have the parent set. Just call datain passedv, too.
您正在调用data构造函数 when iddis still""导致文本框仍然为空。改变MyParent属性不会改变这一点。只会passedv。但那时你还没有父集。只需致电data中passedv了。
回答by Anatoly Nikolaev
Try this:
尝试这个:
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
}
Usage:
用法:
In your Main form:
在您的主要形式中:
abc = new GroupsItems();
abc.IDD="sometext";
MainGrid1.Children.Add(abc); //Grid or any other container for your UserControl
回答by Sheridan
In your Bindingexample, your GroupItemclass looks ok, except that you need to pass in the name of the changed property:
在您的Binding示例中,您的GroupItem类看起来不错,只是您需要传入已更改属性的名称:
public string Text
{
get { return _text; }
set
{
if (value != _text)
{
_text = value;
NotifyPropertyChanged("Text");
}
}
}
Now, in GroupsItems, you shouldn't be accessing the TextBox. In WPF, we manipulate the data, not the UI... but as we use Bindingobjects to data bind the data to the UI controls, they automatically update (if we correctly implement the INotifyPropertyChangedinterface).
现在,在 中GroupsItems,您不应该访问TextBox. 在 WPF 中,我们操作的是数据,而不是 UI……但是当我们使用Binding对象将数据绑定到 UI 控件时,它们会自动更新(如果我们正确实现了INotifyPropertyChanged界面)。
So first, let's add a data property into your code behind (which should also implement the INotifyPropertyChangedinterface) like you did in your GroupItemclass:
所以首先,让我们INotifyPropertyChanged像在GroupItem类中所做的那样,在后面的代码中添加一个数据属性(它也应该实现接口):
private GroupItem _item = new GroupItem();
public GroupItem Item
{
get { return _item; }
set
{
if (value != _item)
{
_item = value;
NotifyPropertyChanged("Item");
}
}
}
Now let's try using a Bindingon a TextBox.Textproperty:
现在让我们尝试Binding在TextBox.Text属性上使用 a :
<TextBox Text="{Binding Item.Text}" />
See how we bind the Textproperty of your GroupItemclass to the TextBox.Textproperty... now all we need to do is to change the value of the Item.Textproperty and watch it update in the UI:
看看我们如何将Text您的GroupItem类的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?";
}
Alternatively, you could put this code into your passedvmethod if you are calling that elsewhere in your project. Let me know how you get on.
或者,passedv如果您在项目的其他地方调用它,您可以将此代码放入您的方法中。让我知道你是怎么办的。
UPDATE >>>
更新 >>>
In your GroupItemclass, try changing the initialization to this:
在您的GroupItem课程中,尝试将初始化更改为:
private string _text = "Any text value";
Can you now see that text in the UI when you run the application? If not, then try adding/copying the whole Textproperty into your code behind and changing the TextBoxdeclaration to this:
您现在可以在运行应用程序时在 UI 中看到该文本吗?如果没有,则尝试将整个Text属性添加/复制到您的代码后面并将TextBox声明更改为:
<TextBox Text="{Binding Text}" />
If you can't see the text value now, you've really got problems... you haveimplemented the INotifyPropertyChangedinterface in your code behind haven't you?
如果你现在看不到文本值,你真的有问题......你已经INotifyPropertyChanged在你的代码中实现了接口,不是吗?

