DP not Working
-
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
-
Thanks for your thoughtful response. That fixed it.
If it's not broken, fix it until it is
-
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
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.
-
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.
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
-
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
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.
-
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.
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
-
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
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
-
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.
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
-
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
Yes, you're correct. For some reason I was thinking the underlying source still need to be explicitly hooked, but it does not.
-
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
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"