I have 2 text box in WPF user control and button on WPF form. How can I access those textbox value on the button click event of that main form where I am using the WPF user control
First of all, keep in mind that WPF is not WinForms -- in theory you should data bind your TextBoxes to properties and then change the value of the properties, instead of accessing the TextBoxes directly!
That being said, all you have to do is to name the UserControl and the TextBoxes, and then access them, like this:
Int MyUserControl.xaml:
<TextBox x:Name="myTextBox1"/>
<TextBox x:Name="myTextBox2"/>
In MyWindow.xaml:
<local:MyUserControl x:Name="myUserControlInstance"/>
<Button Content="Click me" Click="Button_Click" />
In MyWindow.xaml.cs:
private void Button_Click(object sender, RoutedEventArgs e) {
myUserControl.myTextBox1.Text = "Foo";
myUserControl.myTextBox2.Text = "Bar";
}
In the usercontrol, make two public properties which return a string:
public property Textbox1Text
{
get { return TextBox1.Text; }
}
then the text from the textbox controls is visible to the main form.
Or a better option: have an event that the usercontrol can raise, call it something like TextChanged
. Of course you want a better name for it than that, so let's just pretend that your first textbox is designed for the user to enter a name and call the event NameTextChanged
, then your event will be something like this:
public MainWindow()
{
InitializeComponent();
TextBox1.TextChanged += new TextChangedEventHandler(TextBox1_TextChanged);
}
private void TextBox1_TextChanged(object sender, TextChangedEventArgs e)
{
if (NameTextChanged!= null)
this.NameTextChanged(this, e);
}
public event TextChangedEventHandler NameTextChanged;
or better yet, you could go for a routed event - but stick with the basics first.
Subscribing to an event seems to be a better option as suggested by slugster above. If you use this approach you can have multiple instances of the same user control in same window but handle them differently based on which user control they originate from.
As an example you can have address type user control which can have sender address and recepient address which might have same fields like street, city, state etc. But can behave differently when sender address or receipeint address is updated.
Hope this helps.
Nilesh Gule
http://nileshgule.blogspot.com
For your specific problem, I can suggest you a specific solution. This cannot be treated as a general one.
Your problem is to read the contents of the textboxes in your user control on a button click.
Here is the solution.
In this solution, there will be two xaml files and their respective .cs files.
Logic:- The inherent logic is to iterate through the visual ekements in the user control, find the textboxes, read the text in it on button click.
So here is the code...
Window.xaml - This is our main window. This contains 1 button and the object reference of the user control.
<Grid> <StackPanel Orientation="Vertical"> <Button x:Name="clickThis" Height="30" Width="70" Content="Click Me!!" Click="clickThis_Click" /> <local:TxtBoxedUC x:Name="UC" /> </StackPanel> </Grid>
TxtBoxedUC.xaml - This is our user control. This contains our two textboxes.
<Grid> <StackPanel Orientation="Vertical"> <TextBox x:Name="txt1" Width="150" Height="30" /> <TextBox x:Name="txt2" Width="150" Height="30" /> </StackPanel> </Grid>
Window1.xaml.cs - This contains the button click method as well as the method to iterate through the visual elements in the user control.
private void clickThis_Click(object sender, RoutedEventArgs e) { GetVisual(UC); }
The above code is to handle the button click.
private void GetVisual(UIElement currentVisual)
{
int count = VisualTreeHelper.GetChildrenCount(currentVisual);
if (count > 0)
{
for (int i = 0; i < count; i++)
{
UIElement uiElement = VisualTreeHelper.GetChild(currentVisual, i) as UIElement;
if (uiElement != null)
{
if (uiElement.GetType() == typeof(TextBox))
{
TextBox txt = uiElement as TextBox;
MessageBox.Show(txt.Text);
}
}
GetVisual(uiElement);
}
}
}
Above code is to iterate through the visual elements in the user control.
The .cs file of the user control is not needed.
Now, when you click on the button, you can see waht you have entered in the MessageBox.
Happy coding...
Please mark as answer if this solves your problem.