Resolving Binding for Dependency Property Returns Empty String
Posted: 03 Jun 2025, 19:52
Hello! I have a Dependency Property on a custom user control and I am trying to set the value using a Binding. The string is correctly saved to the DataContext but the value of the Dependency Property returned by GetValue() in `OnLoaded` is an empty string.
The issue seems to be with how the dependency property itself is setup because in the adjacent text block the string displays correctly. Additionally, hard coding the string value like: <local:InputActionGlyph Action="MyActionName"/> also works. What might be going wrong here?
Code Behind:
View:
The issue seems to be with how the dependency property itself is setup because in the adjacent text block the string displays correctly. Additionally, hard coding the string value like: <local:InputActionGlyph Action="MyActionName"/> also works. What might be going wrong here?
Code Behind:
Code: Select all
public partial class InputActionGlyph : UserControl
{
public static readonly DependencyProperty ActionProperty = DependencyProperty.Register(
"Action", typeof(string), typeof(InputActionGlyph));
public void OnLoaded(object sender, EventArgs args)
{
var a = (string)GetValue(ActionProperty); // The value of a is empty string
}
}
Code: Select all
<local:InputActionGlyph Action="test"/> <!-- This works -->
<local:InputActionGlyph Action="{Binding MyActionName}"/> <!-- This doesn't work -->
<TextBlock Text="{Binding MyActionName}"/> <!-- This works -->