Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. DP not Working

DP not Working

Scheduled Pinned Locked Moved WPF
helptutorial
11 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kevin Marois
    wrote on last edited by
    #1

    This is a very simple example, and I can't get it to work My UserControl

    Code Behind

    public partial class MonthViewDayColumnHeader : UserControl
    {
    #region DP DayName
    public static readonly DependencyProperty DayNameCaptionProperty =
    DependencyProperty.Register("DayNameCaption",
    typeof(string),
    typeof(MonthViewDayColumnHeader),
    new PropertyMetadata("Today",
    new PropertyChangedCallback(OnDayNameCaptionChanged),
    new CoerceValueCallback(CoerceDayNameCaption)));

    public string DayNameCaption
    {
        get { return (string)GetValue(DayNameCaptionProperty); }
        set { SetValue(DayNameCaptionProperty, value); }
    }
    
    private static void OnDayNameCaptionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        MonthViewDayColumnHeader control = (MonthViewDayColumnHeader)d;
        var dayNameCaption = control.DayNameCaption;  // dayNameCaption is null
    }
    
    private static object CoerceDayNameCaption(DependencyObject d, object value)
    {
        MonthViewDayColumnHeader control = (MonthViewDayColumnHeader)d;
        var dayNameCaption = control.DayNameCaption;   // dayNameCaption is 'Today', the default value
        return null;
    }
    #endregion
    
    
    
    #region CTOR
    public MonthViewDayColumnHeader()
    {
        InitializeComponent();
    
        this.DataContext = this;
    }
    #endregion
    

    }

    Usage:

    The text "Monday" doesn't show up. WTF IS WRONG HERE!!!!

    If it's not broken, fix it until it is

    L P M 3 Replies Last reply
    0
    • K Kevin Marois

      This is a very simple example, and I can't get it to work My UserControl

      Code Behind

      public partial class MonthViewDayColumnHeader : UserControl
      {
      #region DP DayName
      public static readonly DependencyProperty DayNameCaptionProperty =
      DependencyProperty.Register("DayNameCaption",
      typeof(string),
      typeof(MonthViewDayColumnHeader),
      new PropertyMetadata("Today",
      new PropertyChangedCallback(OnDayNameCaptionChanged),
      new CoerceValueCallback(CoerceDayNameCaption)));

      public string DayNameCaption
      {
          get { return (string)GetValue(DayNameCaptionProperty); }
          set { SetValue(DayNameCaptionProperty, value); }
      }
      
      private static void OnDayNameCaptionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
      {
          MonthViewDayColumnHeader control = (MonthViewDayColumnHeader)d;
          var dayNameCaption = control.DayNameCaption;  // dayNameCaption is null
      }
      
      private static object CoerceDayNameCaption(DependencyObject d, object value)
      {
          MonthViewDayColumnHeader control = (MonthViewDayColumnHeader)d;
          var dayNameCaption = control.DayNameCaption;   // dayNameCaption is 'Today', the default value
          return null;
      }
      #endregion
      
      
      
      #region CTOR
      public MonthViewDayColumnHeader()
      {
          InitializeComponent();
      
          this.DataContext = this;
      }
      #endregion
      

      }

      Usage:

      The text "Monday" doesn't show up. WTF IS WRONG HERE!!!!

      If it's not broken, fix it until it is

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Over-engineering.

      K 1 Reply Last reply
      0
      • L Lost User

        Over-engineering.

        K Offline
        K Offline
        Kevin Marois
        wrote on last edited by
        #3

        Thanks for your thoughtful response. That fixed it.

        If it's not broken, fix it until it is

        1 Reply Last reply
        0
        • K Kevin Marois

          This is a very simple example, and I can't get it to work My UserControl

          Code Behind

          public partial class MonthViewDayColumnHeader : UserControl
          {
          #region DP DayName
          public static readonly DependencyProperty DayNameCaptionProperty =
          DependencyProperty.Register("DayNameCaption",
          typeof(string),
          typeof(MonthViewDayColumnHeader),
          new PropertyMetadata("Today",
          new PropertyChangedCallback(OnDayNameCaptionChanged),
          new CoerceValueCallback(CoerceDayNameCaption)));

          public string DayNameCaption
          {
              get { return (string)GetValue(DayNameCaptionProperty); }
              set { SetValue(DayNameCaptionProperty, value); }
          }
          
          private static void OnDayNameCaptionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
          {
              MonthViewDayColumnHeader control = (MonthViewDayColumnHeader)d;
              var dayNameCaption = control.DayNameCaption;  // dayNameCaption is null
          }
          
          private static object CoerceDayNameCaption(DependencyObject d, object value)
          {
              MonthViewDayColumnHeader control = (MonthViewDayColumnHeader)d;
              var dayNameCaption = control.DayNameCaption;   // dayNameCaption is 'Today', the default value
              return null;
          }
          #endregion
          
          
          
          #region CTOR
          public MonthViewDayColumnHeader()
          {
              InitializeComponent();
          
              this.DataContext = this;
          }
          #endregion
          

          }

          Usage:

          The text "Monday" doesn't show up. WTF IS WRONG HERE!!!!

          If it's not broken, fix it until it is

          P Offline
          P Offline
          PureNsanity
          wrote on last edited by
          #4

          The reason why it's not working is the MonthViewDayColumnHeader class doesn't support INotifyPropertyChanged. It's being initialized, then updated with Monday after. If you take the existing and default it to 'Sunday' you should be able to see the default value. On that note, I know it's an example but you should probably start off with MVVM in your learning process. Have the underlying layers implement the INotifyPropertyChanged and set the context to an appropriate view model.

          K Richard DeemingR 2 Replies Last reply
          0
          • P PureNsanity

            The reason why it's not working is the MonthViewDayColumnHeader class doesn't support INotifyPropertyChanged. It's being initialized, then updated with Monday after. If you take the existing and default it to 'Sunday' you should be able to see the default value. On that note, I know it's an example but you should probably start off with MVVM in your learning process. Have the underlying layers implement the INotifyPropertyChanged and set the context to an appropriate view model.

            K Offline
            K Offline
            Kevin Marois
            wrote on last edited by
            #5

            Actually, it wasn't working because I'm a dumba$$...

            private static object CoerceDayNameCaption(DependencyObject d, object value)
            {
            MonthViewDayColumnHeader control = (MonthViewDayColumnHeader)d;
            var dayNameCaption = control.DayNameCaption;value
            return null; //<=== ALWAYS RETURNS NULL
            }

            changed it to

            private static object CoerceDayNameCaption(DependencyObject d, object value)
            {
            MonthViewDayColumnHeader control = (MonthViewDayColumnHeader)d;
            var dayNameCaption = control.DayNameCaption;value
            return dayNameCaption ; //<=== NOW IT WORKS
            }

            If it's not broken, fix it until it is

            P Richard DeemingR 2 Replies Last reply
            0
            • K Kevin Marois

              Actually, it wasn't working because I'm a dumba$$...

              private static object CoerceDayNameCaption(DependencyObject d, object value)
              {
              MonthViewDayColumnHeader control = (MonthViewDayColumnHeader)d;
              var dayNameCaption = control.DayNameCaption;value
              return null; //<=== ALWAYS RETURNS NULL
              }

              changed it to

              private static object CoerceDayNameCaption(DependencyObject d, object value)
              {
              MonthViewDayColumnHeader control = (MonthViewDayColumnHeader)d;
              var dayNameCaption = control.DayNameCaption;value
              return dayNameCaption ; //<=== NOW IT WORKS
              }

              If it's not broken, fix it until it is

              P Offline
              P Offline
              PureNsanity
              wrote on last edited by
              #6

              Yes and no. You shouldn't be using a callback in this case. The callback is circumventing the whole point of the binding. Taking my advice would fix it the appropriate way.

              K 1 Reply Last reply
              0
              • P PureNsanity

                Yes and no. You shouldn't be using a callback in this case. The callback is circumventing the whole point of the binding. Taking my advice would fix it the appropriate way.

                K Offline
                K Offline
                Kevin Marois
                wrote on last edited by
                #7

                Well, actually, I put in the callback for testing and ended up breaking my code ;P

                If it's not broken, fix it until it is

                1 Reply Last reply
                0
                • K Kevin Marois

                  Actually, it wasn't working because I'm a dumba$$...

                  private static object CoerceDayNameCaption(DependencyObject d, object value)
                  {
                  MonthViewDayColumnHeader control = (MonthViewDayColumnHeader)d;
                  var dayNameCaption = control.DayNameCaption;value
                  return null; //<=== ALWAYS RETURNS NULL
                  }

                  changed it to

                  private static object CoerceDayNameCaption(DependencyObject d, object value)
                  {
                  MonthViewDayColumnHeader control = (MonthViewDayColumnHeader)d;
                  var dayNameCaption = control.DayNameCaption;value
                  return dayNameCaption ; //<=== NOW IT WORKS
                  }

                  If it's not broken, fix it until it is

                  Richard DeemingR Offline
                  Richard DeemingR Offline
                  Richard Deeming
                  wrote on last edited by
                  #8

                  I assume the rogue ;value is just a typo in your message?

                  var dayNameCaption = control.DayNameCaption;value // <-- This won't compile.


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                  1 Reply Last reply
                  0
                  • P PureNsanity

                    The reason why it's not working is the MonthViewDayColumnHeader class doesn't support INotifyPropertyChanged. It's being initialized, then updated with Monday after. If you take the existing and default it to 'Sunday' you should be able to see the default value. On that note, I know it's an example but you should probably start off with MVVM in your learning process. Have the underlying layers implement the INotifyPropertyChanged and set the context to an appropriate view model.

                    Richard DeemingR Offline
                    Richard DeemingR Offline
                    Richard Deeming
                    wrote on last edited by
                    #9

                    You don't need to implement INotifyPropertyChanged when you're using dependency properties. DependencyProperties or INotifyPropertyChanged ?[^]


                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                    "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                    P 1 Reply Last reply
                    0
                    • Richard DeemingR Richard Deeming

                      You don't need to implement INotifyPropertyChanged when you're using dependency properties. DependencyProperties or INotifyPropertyChanged ?[^]


                      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                      P Offline
                      P Offline
                      PureNsanity
                      wrote on last edited by
                      #10

                      Yes, you're correct. For some reason I was thinking the underlying source still need to be explicitly hooked, but it does not.

                      1 Reply Last reply
                      0
                      • K Kevin Marois

                        This is a very simple example, and I can't get it to work My UserControl

                        Code Behind

                        public partial class MonthViewDayColumnHeader : UserControl
                        {
                        #region DP DayName
                        public static readonly DependencyProperty DayNameCaptionProperty =
                        DependencyProperty.Register("DayNameCaption",
                        typeof(string),
                        typeof(MonthViewDayColumnHeader),
                        new PropertyMetadata("Today",
                        new PropertyChangedCallback(OnDayNameCaptionChanged),
                        new CoerceValueCallback(CoerceDayNameCaption)));

                        public string DayNameCaption
                        {
                            get { return (string)GetValue(DayNameCaptionProperty); }
                            set { SetValue(DayNameCaptionProperty, value); }
                        }
                        
                        private static void OnDayNameCaptionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
                        {
                            MonthViewDayColumnHeader control = (MonthViewDayColumnHeader)d;
                            var dayNameCaption = control.DayNameCaption;  // dayNameCaption is null
                        }
                        
                        private static object CoerceDayNameCaption(DependencyObject d, object value)
                        {
                            MonthViewDayColumnHeader control = (MonthViewDayColumnHeader)d;
                            var dayNameCaption = control.DayNameCaption;   // dayNameCaption is 'Today', the default value
                            return null;
                        }
                        #endregion
                        
                        
                        
                        #region CTOR
                        public MonthViewDayColumnHeader()
                        {
                            InitializeComponent();
                        
                            this.DataContext = this;
                        }
                        #endregion
                        

                        }

                        Usage:

                        The text "Monday" doesn't show up. WTF IS WRONG HERE!!!!

                        If it's not broken, fix it until it is

                        M Offline
                        M Offline
                        Meshack Musundi
                        wrote on last edited by
                        #11

                        It should be,

                        <TextBlock Text="{Binding DayNameCaption, RelativeSource={RelativeSource FindAncestor, AncestorType={UserControl}}}"... />

                        You don't need the callback.

                        "As beings of finite lifespan, our contributions to the sum of human knowledge is one of the greatest endeavors we can undertake and one of the defining characteristics of humanity itself"

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups