简体   繁体   中英

DependencyProperty propery not called in windows phone 8

I previously posted a problem based on the same problem but I thought I'd start again with more accurate information as there were too many factors I thought were at the source of the problem which weren't actually relevant.

I have a UserControl in a windows phone app in which I need to add a property that will be bound to a property declared in my ViewModel . From what I've read on the net and I understood, I need to create a DependencyProperty in order to make it bindable.

public static readonly DependencyProperty SourceFileProperty = DependencyProperty.
Register("SourceFile", typeof(string), typeof(ViewerControl), 
new PropertyMetadata(""));

public string SourceFile
{
    get
    {
         return base.GetValue(SourceFileProperty) as string;
    }
    set
    {
         base.SetValue(SourceFileProperty, value);
    }
}

This is the property in my ViewModel :

public string DocumentUri
{
    get { return this._documentUri; }
    set
    {
        if (this._documentUri != value)
        {
            this.SetProperty(ref this._documentUri, value);
        }
    }
}

Note the SetProperty takes take of the INotifyPropertyChange and my ViewModel is definitely bound as other properties are used and are updating correctly but these are not bound to my user control.

This is my XAML

<Grid Grid.Row="1">
    <StackPanel Orientation="Vertical">
        <cc:ViewerControl SourceFile="{Binding DocumentUri}" />
    </StackPanel>                        
</Grid>

I've got a breakpoint on the getter and setter of my DocumentUri in my ViewModel and the SourceFile in the UserControl and on the definition of the DependencyProperty .

When I run my code, the DocumentUri is called accordingly, the definition of the DependencyProperty is also initialized but it never calls the getter/setter of the SourceFile property in my UserControl .

NOTE: If I define the property as a regular CLR property, I get an error which makes no sense:

Value does not fall within the expected range

I need the SourceFile property to be "triggered" when the DocumentUri property is changed.

What I am doing wrong or missing???

Thanks.

Dependency property getter and setter never gets called when DP is get/set from XAML . That;s why you shouldn't put any code in there.

From MSDN link:

The current WPF implementation of its XAML processor is inherently dependency property aware. The WPF XAML processor uses property system methods for dependency properties when loading binary XAML and processing attributes that are dependency properties. This effectively bypasses the property wrappers. When you implement custom dependency properties, you must account for this behavior and should avoid placing any other code in your property wrapper other than the property system methods GetValue and SetValue.

You can use PropertyChangedCallback to track DP property changes which gets called whenever DP property changes from XAML or from code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM