I am not sure if this is even possible, but here is my setup:
I have a customized textbox that is only meant to deal with hotkey selection. In here I have a DependencyProperty that is for the SelectedHotKey.
Then I have a custom control that is a label, the textbox, and a button. This control also exposes a DependencyProperty of the same name as the TextBox, and it merely ties to the TextBox via:
SelectedHotKey="{Binding ElementName=Main, Path=SelectedHotKey, Mode=TwoWay}"
With this dependency property, I am looking to set the textbox’s SelectedHotKey, which will change the Text appropriately.
I am then using that user control and binding to my ViewModel.
I have all of this working except for the initialization case.
When my ViewModel is already set up and passed to the UserControl in the binding, that initial setter does not even get hit, so it does not propogate down through my controls. I thought about putting in a PropertyMetaData method, however that is a static method and I do not have access to my instance textboxes.
Any ideas? Let me know if I need to clarify further.
Don’t put code logic in the CLR property wrapping the dependency property. As you have already seen, there is no guarantee they will ever be called since the framework can call
SetValuedirectly on the dependency property.You’re in the right track: you have to use the dependency property metadata. Access the instance by using the first parameter of the property changed callback.