Hai
am having a WPF user control in my WPF Form, when i click开发者_开发知识库 my button in my form , i just want to pass some value to the textbox which is in the usercontrol, tell me how to do this.
There are several ways you can do this. The easiest way is to use a String property and implement INotifyPropertyChanged in your UserControl.
To illustrate, you will have your UserControl like so:
/// <summary>
/// Interaction logic for TextBoxUsercontrol.xaml
/// </summary>
public partial class TextBoxUsercontrol : UserControl, INotifyPropertyChanged
{
private string _text;
public string Text
{
get { return _text; }
set
{
_text = value;
if(PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Text"));
}
}
public TextBoxUsercontrol()
{
DataContext = this;
InitializeComponent();
}
public event PropertyChangedEventHandler PropertyChanged;
}
Now the TextBox in your UserControl must bind itself to your Text property like so:
<TextBox Text="{Binding Text}" />
Then in your WPF form, you will have your UserControl declared and a button to handle the click like so:
<local:TextBoxUsercontrol x:Name="textBox" />
<Button Click="ButtonBase_OnClick" >Add Text</Button>
And finally, in your Click handler:
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
textBox.Text = "Hello!";
}
Having shown you the solution, I give your question asking skill a 1 out of 5. You can be a lot more specific with your question and give sample codes snippets as I have done without asking us to download your whole solution from a site that we must wait download it (not to mention most of us are security conscious about downloading unknown files).
Good luck.
Standard WPF? NO WAY.
Why? You dont pass values around. You get a click event on the item that is clicked (the button) with elements defined on the button (only), then in the code you access the other elements, which thus also have to be either defined in code (the standard way) and expose their values through something called "properties", OR you get the control by name and extract the value. But you dont pass any additional data around.
Look at the tutorials ;)
If you want to PASS values around to METHODS on a click, you need to use something like caliburn (http://www.codeplex.com/caliburn) which allows you to map the click to a method and grab the values passed into the method from other controls.
Just Create a Dependency property and Bind the Porperty to the UserControl's TextBox. While creating the object itself assign the value to the Usercontrol's dependency property.
精彩评论