Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
It's late and the answer is probably something simple so apologies if this is a dumb question.

I have a user control, it had a single dependency property which is set via a binding which works fine.

I then added two more but the new two (which are setup exactly the same as the first) will not populate from XAML. They always contain their default values (null/0.0).

User control:

public partial class MyUserControl : UserControl
{
    public static readonly DependencyProperty WorkingValueProperty;
    public static readonly DependencyProperty VarBValueProperty;
    public static readonly DependencyProperty VarCValueProperty;
    public MyUserControl()
    {
        InitializeComponent();
    }
    static MyUserControl()
    {
        FrameworkPropertyMetadata WorkingValueMetadata = new FrameworkPropertyMetadata(new PropertyChangedCallback(WorkingValuePropertyChanged));
        FrameworkPropertyMetadata VarBValueMetadata = new FrameworkPropertyMetadata();
        FrameworkPropertyMetadata VarCValueMetadata = new FrameworkPropertyMetadata();
        MyUserControl.WorkingValueProperty = DependencyProperty.Register("WorkingValue", typeof(double), typeof(MyUserControl), WorkingValueMetadata);
        MyUserControl.VarBValueProperty = DependencyProperty.Register("VarBValue", typeof(double), typeof(MyUserControl), VarBValueMetadata);
        MyUserControl.VarCValueProperty = DependencyProperty.Register("VarCValue", typeof(string), typeof(MyUserControl), VarCValueMetadata);
    }
    public double WorkingValue
    {
        get
        {
            return (double)GetValue(WorkingValueProperty);
        }
        set
        {
            SetValue(WorkingValueProperty, value);
        }
    }
    public double VarBValue
    {
        get
        {
            return (double)GetValue(VarBValueProperty);
        }
        set
        {
            SetValue(VarBValueProperty, value);
        }
    }
    public string VarCValue
    {
        get
        {
            return (string)GetValue(VarCValueProperty);
        }
        set
        {
            SetValue(VarCValueProperty, value);
        }
    }
    private static void WorkingValuePropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
    {
        MyUserControl myUC = (MyUserControl)o;
    ...

    }


XAML:

<ic:myusercontrol workingvalue="{Binding AnotherValue, UpdateSourceTrigger=PropertyChanged}" varcvalue="Binding AnotherValue, UpdateSourceTrigger=PropertyChanged}" xmlns:ic="#unknown">
                     VarBValue="200"/></ic:myusercontrol>


The user control is in a DataTemplate which is used in a listbox. It's not really meant to be bound to the same value as the first field but I know for sure that one works as it's populating the first property fine. If I create the control and just hard code the value all the properties populate fine so it's something to do with it being in a list but I can't figure it out.

I'm looking at the values in call back WorkingValuePropertyChanged (it's there for another purpose but it seemed a reasonable place to check). WorkingValueProperty is populated and the other two are null and 0.0 respectively.

Thanks in advance.

Bob
Posted

1 solution

The properties are set one after the other, and when WorkingValuePropertyChanged is called, it's likely that only the first property has been set. So you may be examining those properties too early.
 
Share this answer
 
Comments
rj_hogan 8-Feb-11 5:12am    
Yup, that was the problem. Thanks for the assist!
Nish Nishant 8-Feb-11 8:29am    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900