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. Trying to bind to a dependency property [modified]

Trying to bind to a dependency property [modified]

Scheduled Pinned Locked Moved WPF
wpfhelpwcfcomdesign
2 Posts 2 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.
  • C Offline
    C Offline
    Christian Graus
    wrote on last edited by
    #1

    I have a custom control, called 'YesNoButton'. It's two buttons, one says yes, one says no. I have a property, 'IsYes'. I'd like to bind to that property from my preferences class, so that I don't have to write any code to tie the two. Here's what I have:

    is my XAML with my attempted binding. The code in the control is as follows ( created with help from the tab/tab auto generate thing )

        public bool IsYes
    
        {
    
            get { return (bool)GetValue(IsYesProperty); }
    
            set 
    
            {
    
                SetValue(IsYesProperty, value);
    
                no.Foreground = (value) ? Brushes.LightGray : Brushes.Black;
    
                yes.Foreground = (value) ? Brushes.Black : Brushes.LightGray;
    
            }
    
        }
    
    
    
        public static readonly DependencyProperty IsYesProperty =
    
            DependencyProperty.Register("IsYes", typeof(bool), typeof(YesNoButton), new UIPropertyMetadata(false));
    

    This code compiles and runs, so I assume that my binding is where the issue is. Preferences is a static class, with Instance as a static variable. If I set it in code, it works, internally, but does not change the UI on this form. If I set the default of the dependency property to 'true' it doesn't change the UI. If I set breakpoints, my setter is called when the value is set in code, and the values in the preferences class get set. My issue is entirely that I can't get the control to call the right getter on the preferences class to set itself up initially, that is, it always shows the same value and does not respect the value in the prefs class.

    OK, I got it. I had to wait for the Loaded event, THEN I had to access the property to force it to read from the source, which then ran the code to set the state of the UI. If there's any way to make it read automatically, I'd love to know it.

    Thanks for any help.

    Christian Graus

    Driven to the arms of OSX by Vista.

    Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

    modified on Thursday, September 2, 2010 2:04 AM

    P 1 Reply Last reply
    0
    • C Christian Graus

      I have a custom control, called 'YesNoButton'. It's two buttons, one says yes, one says no. I have a property, 'IsYes'. I'd like to bind to that property from my preferences class, so that I don't have to write any code to tie the two. Here's what I have:

      is my XAML with my attempted binding. The code in the control is as follows ( created with help from the tab/tab auto generate thing )

          public bool IsYes
      
          {
      
              get { return (bool)GetValue(IsYesProperty); }
      
              set 
      
              {
      
                  SetValue(IsYesProperty, value);
      
                  no.Foreground = (value) ? Brushes.LightGray : Brushes.Black;
      
                  yes.Foreground = (value) ? Brushes.Black : Brushes.LightGray;
      
              }
      
          }
      
      
      
          public static readonly DependencyProperty IsYesProperty =
      
              DependencyProperty.Register("IsYes", typeof(bool), typeof(YesNoButton), new UIPropertyMetadata(false));
      

      This code compiles and runs, so I assume that my binding is where the issue is. Preferences is a static class, with Instance as a static variable. If I set it in code, it works, internally, but does not change the UI on this form. If I set the default of the dependency property to 'true' it doesn't change the UI. If I set breakpoints, my setter is called when the value is set in code, and the values in the preferences class get set. My issue is entirely that I can't get the control to call the right getter on the preferences class to set itself up initially, that is, it always shows the same value and does not respect the value in the prefs class.

      OK, I got it. I had to wait for the Loaded event, THEN I had to access the property to force it to read from the source, which then ran the code to set the state of the UI. If there's any way to make it read automatically, I'd love to know it.

      Thanks for any help.

      Christian Graus

      Driven to the arms of OSX by Vista.

      Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

      modified on Thursday, September 2, 2010 2:04 AM

      P Offline
      P Offline
      PumbaPumba
      wrote on last edited by
      #2

      Hi Christian Graus, Included UpdateSourceTrigger="PropertyChanged" in binding. Like,

      "showProgessOnDesktop" IsYes="{Binding Mode=TwoWay, Path=ShowOnDesktop, Source={x:Static c:Preferences.Instance}, UpdateSourceTrigger=PropertyChanged}"

      HTH

      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