开发者

Dependency property not updating Silverlight 4

I'm having some trouble updating a dependency property of a UserControl/View.

I have a main view (MainView.xaml) that has a series of other user controls declared inside it. One of them looks as follows:

<local:Snapshot BrandID="{Binding Path=Session.Test}" />

My Snapshot.xaml has a a TextBlock:

<TextBlock Text="Sample text" x:Name="brandIDTBlock" />

And my Snapshot.xaml.cs has the following dependency property:

public string BrandID
{
    get { return (string)GetValue(BrandIdProperty); }
    set { SetValue(BrandIdProperty, value); }
}

public static readonly DependencyProperty BrandIdProperty = DependencyProperty.Register("BrandID", typeof(string), typeof(Snapshot), new PropertyMetadata(new PropertyChangedCallback(OnBrandIdChange)));

private static void OnBrandIdChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    var control = (Snapshot)d;
    control.brandIDTBlock.Text = (string)e.NewValue;
}

Session is a property of the MainView and I want my brandIDTBlock TextBlock to be updated when the Test property is changed in the Session object.

The Test property is declared like so:

private string _test = "Test Value Binding";

public string Test 
{
    get { return _test; }
    set { _test = value; } 
}

Right now somehting is working as when I run the app I see "Test Value Binding" correctly being displayed in my vi开发者_开发知识库ew, the problem is that when the value of Test changes in my Session object the change does not propagate to the view.

I also tried to implement the INotifyPropertyChanged interface like so:

public string Test
{
    get { return _test; }
    set
    {
        _test = value;
        OnPropertyChanged("Test");
    }
}

public event PropertyChangedEventHandler PropertyChanged;

protected void OnPropertyChanged(string name)
{
    PropertyChangedEventHandler handler = PropertyChanged;
    if (handler != null)
    {
        handler(this, new PropertyChangedEventArgs(name));
    }
}

but it's still not updating.

UPDATE: It turns out that it was a nooby mistake, again. In my Snapshot.xaml.cs I was changing the data context in a piece code that handled a pie chart. I worked around it by changing my binding epression in so that the binding element would be the parent of Snapshot.xaml, in my case the StackPanel 'sp' ({Binding ElementName=sp, Path=DataContext.Session.Test}). Apart from this silly mistake, what was really missing from my original code was the INotifyPropertyChanged implementation, TwoWay binding was unnecessary.

Thanks

Luis


You need to add Mode=TwoWay to your binding:

<local:Snapshot BrandID="{Binding Path=Session.Test, Mode=TwoWay}" />

OneWay is the default and that allows the UI to update the bound value only. To get the UI to change after the bound value is changed you need TwoWay.

You also need to raise the property changed event to let the UI know that something has changed.

The only other thing is to check that you've set the DataContext of your view up correctly.


The binding is not correct. By using a {Binding} with just a path you try to bind to the data exposed through the DataContext. Did you set the DataContext?

EDIT

There is more wrong in your code:

control.brandIDTBlock.Text = (string)e.NewValue;

This line does not belong in the PropertyChanged handler. And might cause you to think that binding is working.

I do not have the time to add a correct example so here is a link that should get you started.


When the value of Test changes in your view model is it "changing" because you have replaced the object being returned the the Session property of your main view model. If so you would need the main view model to alse implement INotifyPropertyChanged and call PropertyChanged when the Session property is updated.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜