0

My UserControl contains a TextBox and a Button. The TextBox's Text is correctly populated by a dependency property called X.

My Goal: Change the value of X (e.g. Text of the TextBox) when I press the Button.

I have defined the UserControl as follows:

<StackPanel Orientation="Horizontal" >
    <TextBox Name="Xbox" Text="{Binding Path=X}" Width="50"/>
    <Button Content="Current" Click="InsertCurrentBtnClick" />
</StackPanel>

With codebehind:

    public double X
    {
        get { return (double)GetValue(XProperty); }
        set { SetValue(XProperty, value); }
    }

    public static readonly DependencyProperty XProperty =
        DependencyProperty.Register("X", typeof(double), typeof(MyUserControl), new PropertyMetadata(0.0));


    private void InsertCurrentBtnClick(object sender, RoutedEventArgs e)
    {
        X = 0.7;

        //BindingOperations.GetBindingExpression(this, XProperty).UpdateTarget();
        //BindingOperations.GetBindingExpression(Xbox, TextBox.TextProperty).UpdateTarget();
        //BindingOperations.GetBindingExpression(Xbox, XProperty).UpdateTarget();
        //Xbox.GetBindingExpression(TextBox.TextProperty).UpdateTarget();
        //GetBindingExpression(XProperty).UpdateTarget();
    }

I have tried several things - one at a time - (see below X=0.7;) to force the update to the TextBox Text but nothing has helped so far.

Thanks in advance.

3 Answers 3

2

I'd write it in this way:

    public double X
    {
        get { return (double)GetValue(XProperty); }
        set { SetValue(XProperty, value); }
    }

    public static readonly DependencyProperty XProperty =
        DependencyProperty.Register("X", typeof(double), typeof(MainPage), new PropertyMetadata(new PropertyChangedCallback(Callback)));


    public static void Callback(DependencyObject o, DependencyPropertyChangedEventArgs e)
    {
        (o as MainPage).Xbox.Text = e.NewValue.ToString();
    }

    private void InsertCurrentBtnClick(object sender, RoutedEventArgs e)
    {
        X = 0.7;
    }

And the xaml code:

    <StackPanel Orientation="Horizontal" >
        <TextBox Name="Xbox" Width="50"/>
        <Button Content="Current" Click="InsertCurrentBtnClick" />
    </StackPanel>
Sign up to request clarification or add additional context in comments.

Comments

1

You need to set the DataContext for you Control. As I see X defined in your control, you need to do this :

    public MyUserControl()
    {
        InitializeComponent();

        // add this line
        this.DataContext = this;
    }

2 Comments

welcome. DataContext and DataBinding are made to work together :)
Oops, it seems I assessed this answer quickly. If I use this, my X gets no longer populated as a dependency property. It doesn't get whatever value is passed to it with <MyUserControl X={Binding Value} />
1

Although, you can bind it as well, just change the xaml:

<UserControl x:Class="SilverlightApplication1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Name="myWidnow"
d:DesignHeight="300" d:DesignWidth="400">

<Grid x:Name="LayoutRoot" Background="White">
    <StackPanel Orientation="Horizontal" >
        <TextBox Name="Xbox" Width="50" Text="{Binding ElementName=myWidnow, Path=X}" />
        <Button Content="Current" Click="InsertCurrentBtnClick" />
    </StackPanel>
</Grid>

Notice that I've added the Name proeprty to the UserControl. In this case, you don't have to change anything in the code behid.

2 Comments

Oops, it seems I assessed this answer quickly. If I use this, my X gets no longer populated as a dependency property. It doesn't get whatever value is passed to it with <MyUserControl X={Binding Value} />
Well you need to implement Value also as a dependencyProperty or implement INotigyPropertyChanged. The one using your UserConyrol needs to use DataContext as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.