0

I have a custom object type dependency property that is being populated at runtime. It is updated correctly, and the corect information is being stored in this object.

However, when I try to bind a property of this object to a textblock, no text appears (despite it definitely having data).

So, let's say the object type is as follows :

Public class CustomObject{
[Key]
 public int Id { get; set; }
 public string Name { get; set; }
 }

This object is definitely being populated, I have verified this with breakpoints.

I create a dependency property of this objects type, which definitely works correctly - this has also been verified. Let's call the dependency property SelectedCustomObject.

And I bind this dependency property to my view :

 <TextBlock  Text="{Binding SelectedCustomObject.Name, Mode=TwoWay}" FontSize="10" VerticalAlignment="Center" HorizontalAlignment="Center"/>

The DataContext is set in my user control, and this definitely works as I have made several bindings to other dependency properties in the same view model and they work perfectly.

Therefore, by process of elimination I can only assume that my XAML syntax is off and you cannot bind to my populated and working dependency property like this.

Can anyone shed some light on this for me please ?

3 Answers 3

1

The answer to my question turned out to be that Silverlight was maintaining a separate cache and thus my new dependency property was, for whatever reason, not loading.

I deleted and regenerated my .XAP file, nothing.

I cleared my IE cache, nothing.

I opened Microsoft Silverlight from my start menu, went to the Application Storage tab and deleted all - then when I refreshed, all of my work appeared.

Happy days.

Hope this helps someone else :).

0

The CustomClass needs to implement INotifyPropertyChanged so the UI will be notified of changes

1
  • Yes, when the dependency property is set this is called. Jul 12, 2012 at 9:47
0

It is likely that your Name value is being set after your view binds to the property. As @Erno pointed out, your view will not be notified of the change. Implementing INotifyPropertyChanged will allow your CustomObject to notify your view that the property has changed.

Here is what the implementation could look like:

public class CustomObject : INotifyPropertyChanged
{
    [Key]
    public int Id { get; set; }

    private string _name;
    public string Name
    {
        get { return _name; }
        set
        {
            _name = value;
            NotifyPropertyChanged("Name");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged(string info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}
1
  • This is alread being done in my implemenetation. I am using the Galasoft MVVMLight framework :) Jul 12, 2012 at 9:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.