Search code examples
c#wpfbindinguser-controls

Binding to a custom Dependency Property of a UserControl on a Page isn't working


I created a user control, and that has an custom Dependency Property (this property isn't bound in the user control). I am trying to bind that property on the page where I use it. On the application start, the binding is working, but after that I can't update the value.

The value of the binding source is changing and if I bind an object on the page (example the text property of the Text Block) I can see the changes.

Part of my user control:

        public int MaxValue
        {
            get { return (int)GetValue(MaxValueProperty); }
            set
            {
                SetValue(MaxValueProperty, value);
                NotifyPropertyChanged("MaxValue");
            }
        }

        public static readonly DependencyProperty MaxValueProperty =
            DependencyProperty.Register(
                "MaxValue",
                typeof(int),
                typeof(NumericUpDown),
                new PropertyMetadata(
                    5,
                    new PropertyChangedCallback(OnMaxValuePropertyChanged))
                );

Part of my Page:

<local:NumericUpDown x:Name="NudChapter"
                     Height="40"
                     Width="auto"
                     VerticalAlignment="Top"
                     Margin="0,10,0,0"
                     MaxValue="{Binding Path=Model.ChaptersInTheBook}"
                     HintText="Fejezet"/>

Solution

  • I solved my problem. I deleted from my user control ctor the DataContext=this settings and now it's working perfectly.