views:

90

answers:

3

I have a custom property that works perfectly, except when it's bound to an object.

The reason is that once the following code is executed:

base.SetValue(ValueProperty, value);

... then my control is no longer bound. I know this because calling:

base.GetBindingExpression(ValueProperty);

... returns the binding object perfectly - UNTIL I call base.SetValue. So my question is, how do I pass the new "value" on to the object that I'm bound to?

EDIT: Here's the code...

public static readonly DependencyProperty ValueProperty =
    DependencyProperty.Register("Value", typeof(decimal?), typeof(NumericTextBox));

public decimal? Value
{
    get
    {
        return (decimal?)base.GetValue(ValueProperty);
    }
    set
    {
        base.SetValue(ValueProperty, value);
    }
}

As you can see, it's simple, boilerplate, "Microsoft sample" code.

EDIT 2: Here's the code for the binding...

// Apparently, not showing this line of code here has some people confused...
this.DataContext = new SomeClassWithAPropertyCalledNumericValue();

<my:NumericTextBox Value="{Binding NumericValue}" />

Again... simple.

A: 

I think you need to post more code. You're binding to "NumericValue" in XAML which I don't see in your C# code.

As a wild guess, though, if you binding is in fact correct, you may need to move off the textbox in order to see the change (assuming you haven't tested it that way), or make your binding look like this to change with the property change:

{Binding NumericValue, UpdateSourceTrigger=PropertyChanged}
Phil Sandler
A: 

You're binding to NumericValue, but you're not showing us NumericValue. Based purely on the code above, your binding should be something along the lines of:

<my:NumericTextBox Text="{Binding Value}" />

If you really are meaning to bind "Value" to "NumericValue" then you're going to have to show us the DependencyProperty for NumericValue.

McAden
NumericTextBox is my own custom class... it doesn't have a "Text" property, it has a "Value" property. I'm binding my custom class's "Value" property to a decimal called NumericValue which is part of the datacontext that is attached.
Timothy Khouri
And is NumericValue a DependencyProperty?
McAden
A: 

I've found the answer, and the fact that no one else has said this just shows that very few people are making their own custom controls...

1) If you just call "base.SetValue", you WILL destroy any binding applied to your custom property - simple as that.

2) What you have to do is determine if you are bound, if so, update the target manually yourself (really Microsoft?) - and if you're not bound, then you can call base.SetValue.

Here's the helper method I had to make:

private void SetValueOrBinding(decimal? value)
{
    var bindingExpression = this.GetBindingExpression(ValueProperty);

    if (bindingExpression == null)
    {
        base.SetValue(ValueProperty, value);
    }
    else
    {
        var target = bindingExpression.DataItem;

        var propertyInfo = target.GetType().GetProperty(bindingExpression.ParentBinding.Path.Path);

        propertyInfo.SetValue(target, value, null);
    }
}

Can you say extension method anyone?

Timothy Khouri