views:

463

answers:

3

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.

A: 

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.

TomTom
i tried that too, but its not working, can u plz take a look to attached project in the given look, here am having a button, when i clik this i will pass some values to usercontrol text box, i can get the value in user control, but i cant able to bind the value to the text boxhttp://www.easy-share.com/1909552056/InfoBox.rar
deep
Nope, sorry, wont do your work for you;) Plus I am behind a corpoarate firewall ;)
TomTom
A: 

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.

Kishore Kumar
i tried that too, but its not working, can u plz take a look to attached project in the given look, here am having a button, when i clik this i will pass some values to usercontrol text box, i can get the value in user control, but i cant able to bind the value to the text boxhttp://www.easy-share.com/1909552056/InfoBox.rar
deep
else can u tell any sample for how to use dependency object for usercontorl??
deep
just donwload the sample and see http://www.zshare.net/download/7380778840f0b8ab/
Kishore Kumar
+1  A: 

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.

Tri Q
sure, here after will do as u said, thanks , its working
deep