Home ยป How do I pass changes from source in a TwoWay binding?

How do I pass changes from source in a TwoWay binding?

public class SampleCustomControl : CustomControl
{
    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }

    public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register(“Text”, typeof(string), typeof(SampleCustomControl), new PropertyMetadata(null));

    public void Update()
    {
        // This replaces my binding, I want it to pass the new value
        // through to the “SomeProperty” two way binding.
        Text = “some value”;
    }

}

<Controls:SampleCustomControl Text=”{Binding SomeProperty, Mode=TwoWay}” />

public class TestControl : ContentControl
{
    private TextBlock _tb;

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        _tb = new TextBlock();
        _tb.Text = Text;
        this.Content = _tb;
        _tb.MouseLeftButtonDown += new MouseButtonEventHandler(_tb_MouseLeftButtonDown);
    }

    void _tb_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        Update();
    }

    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }

    public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register(“Text”, typeof(string), typeof(TestControl), new PropertyMetadata(string.Empty, OnTextChanged));

    public void Update()
    {
        // This replaces my binding, I want it to pass the new value
        // through to the “SomeProperty” two way binding.
        Text = “some value”;
    }

    public static void OnTextChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        ((TestControl)sender).UpdateText((string)e.NewValue);
    }

    protected void UpdateText(string text)
    {
        if (_tb != null) _tb.Text = text;
    }
}