views:

194

answers:

2

I'm on a roll today...

I have the following code delaring a dependency property inside a class called ActionScreen:

#region Dependency Properties & Methods

public string DescriptionHeader
{
    get { return (string)GetValue(DescriptionHeaderProperty); }
    set { SetValue(DescriptionHeaderProperty, value); }
}

// Using a DependencyProperty as the backing store for DescriptionHeader.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty DescriptionHeaderProperty =
    DependencyProperty.Register("DescriptionHeader", typeof(string), typeof(ActionScreen), new UIPropertyMetadata("xxx"));

#endregion

I bind to this property in my Xaml as so:

                    <GridViewColumn DisplayMemberBinding="{Binding Description}" Header="{Binding DescriptionHeader}" Width="350" />

Now I want to be able to set the parameter from my code behind when I recieve an event - but it's not working:

public string DescColText { set { this.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate() { DescriptionHeader = value; })); } }
A: 

I've run into this same issue. It seems that the depedency property change does not fire a notification that the property has in fact changed. You have to do this manually. In your code behind, implement the INotifyPropertyChanged interface and in your dependency property registration, add the PropertyChangedCallback delegate. (example)

public static readonly DependencyProperty DescriptionHeaderProperty = DependencyProperty.Register("DescriptionHeader", typeof(string), typeof(ActionScreen), new UIPropertyMetadata("xxx", new PropertyChangedCallback(DisplayTextChange)));

    private static void DisplayTextChange(DependencyObject dpo, DependencyPropertyChangedEventArgs args)
            {
                ((WhatEverYourClassNameIs)dpo).NotifyPropertyChanged("DescriptionHeader");

            }

Here's the example of the INotifyPropertyChanged implementation

public event PropertyChangedEventHandler PropertyChanged;

private void NotifyPropertyChanged(String info)
{
    if (PropertyChanged != null)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(info));
    }
}

That should allow your UI to see that a change has occured.

Queso
Hmmm this looks so close to what I want. My only issue now is that PropertyChanged is null so it's never firing the event. Where do I add the event handler for this?
Matt B
You wouldn't add a handler for PropertyChanged.Check your Output window. Make sure there are no databinding errors (They would start with "System.Windows.Data Error: ")
Scott J
Not that I can see...
Matt B
A: 

Hi resolved this I was not setting the datacontext of the page.

Apparently I needed to do

this.DataContext = this;
Matt B