Dependency Property assigned with value binding does not work

c#, dependency-properties, windows-8.1, windows-store-apps, winrt-xaml

Solution

It's frustrating isn't it? First, include a changed event handler. Like this:

public string Title
{
    get { return (string)GetValue(TitleProperty); }
    set { SetValue(TitleProperty, value); }
}
public static readonly DependencyProperty TitleProperty =
    DependencyProperty.Register("Title", typeof(string), 
    typeof(MyControl), new PropertyMetadata(string.Empty, Changed));
private static void Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    var c = d as MyControl;
    // now, do something
}

Then, please read this article so you see there are more gotchas than just that one: http://blog.jerrynixon.com/2013/07/solved-two-way-binding-inside-user.html

Best of luck!

Problem

I have a usercontrol with a dependency property. ``` public sealed partial class PenMenu : UserControl, INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } public bool ExpandCollapse { get { return false; } set { //code } } public static readonly DependencyProperty ExpandCollapseProperty = DependencyProperty.Register("ExpandCollapse", typeof(bool), typeof(PenMenu), null); //some more code } ``` And I am assigning value in XAML page as: ``` <Controls:PenMenu x:Name="penMenu" Opened="Menu_Opened" ExpandCollapse="{Binding PenMenuVisible}" /> ``` But it is not hitting GET-SET part of ExpandCollapse property in the usercontrol. So I added bool to bool converter just to check what value is being passed with binding like: ``` <Controls:PenMenu x:Name="penMenu" Opened="Menu_Opened" ExpandCollapse="{Binding PenMenuVisible, Converter={StaticResource booleanToBooleanConverter}}" /> ``` And with breakpoint in Converter, I see the value being passed is correct. What is the possible reason it's not assigned to the Dependency Property? Also in XAML page if I say: ``` <Controls:PenMenu x:Name="penMenu" Opened="Menu_Opened" ExpandCollapse="true"/> ``` then it hits the GET-SET part of ExpandCollapse property in the usercontrol. I am stuck. This is weird. Please help.

Original source

Related problems