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