views:

139

answers:

1

I have a Silverlight custom control with two properties; Text and Id. I have created DependencyProperties for these as per the code below.

public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(LookupControl), new PropertyMetadata(NotifyPropertyChanged));
public static readonly DependencyProperty IdProperty = DependencyProperty.Register("Id", typeof(Guid?), typeof(LookupControl), new PropertyMetadata(NotifyPropertyChanged));

public event PropertyChangedEventHandler PropertyChanged;

public static void NotifyPropertyChanged(object sender, DependencyPropertyChangedEventArgs args)
{
    var control = sender as LookupControl;
    if (control != null && control.PropertyChanged != null)
    {
        control.PropertyChanged(control, new PropertyChangedEventArgs("Text")); 
    }
}
public Guid? Id
{
    get { return (Guid?)GetValue(IdProperty); }
    set { SetValue(IdProperty, value); }
}

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

In a control method, the Id is populated first, and then the Text. My problem is that when i bind to Text and Id on this control, I want their data to be populated syncronously, so that when a PropertyChanged event fires on either property, both of them have updated data.

At this point in time I catch when the Id has changed, performed some processing, and if required, i set the Text to a new value. But once this OnChange of Id has finished, then the control method continues and populates the Text after i have already changed it back to something else.

It might sound a little convoluted, but hopefully it is enough to understand. If you need more info, just ask.

A: 

Cold you save the values and only set when you have both?

    private Guid? id;
    private string text;

    public Guid?Id
    {
        get { return id; }
        set { 
            id = value;
            TrySetValue();
        }
    }
    public string Text 
    { 
        get { return text; }
        set { text = value;
        TrySetValue()} 
    }

    private void TrySetValue()
    {
        if (id != null && text != null)
        {
            SetValue(IdProperty, id);
            SetValue(TextProperty, text);
        }
    }
Mark