aivanov
Topic Author
Posts: 19
Joined: 30 Sep 2024, 17:25

Resolving Binding for Dependency Property Returns Empty String

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:
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
        }
}
View:
<local:InputActionGlyph Action="test"/> <!-- This works -->
<local:InputActionGlyph Action="{Binding MyActionName}"/>	<!-- This doesn't work -->
<TextBlock Text="{Binding MyActionName}"/> 	<!-- This works -->
 
User avatar
sfernandez
Site Admin
Posts: 3363
Joined: 22 Dec 2011, 19:20

Re: Resolving Binding for Dependency Property Returns Empty String

04 Jun 2025, 12:15

Hi,

I replicated your user control and created a small Unity example and everything works as expected.
You can find it here to check what do you have different in your project.
CustomControlTest.unitypackage
(6.12 KiB) Downloaded 985 times
Hope this helps.
 
aivanov
Topic Author
Posts: 19
Joined: 30 Sep 2024, 17:25

Re: Resolving Binding for Dependency Property Returns Empty String

04 Jun 2025, 22:16

Hello! Thank you for your reply! It made me realize that there must be some other code which is preventing the binding from happening correctly and this made me realize that InputActionGlyph also has a model.

The correct binding is actually:
 Action="{Binding DataContext.MyActionName, ElementName=MyGrid}" 
Hopefully this helps someone in the future!

For reference:
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
        }
        
        public InputActionGlyph()
	{
		InitializeComponent();
		DataContext = new InputActionGlyphModel();
		// ...
	}
	
	// ...
}
 
User avatar
jsantos
Site Admin
Posts: 4822
Joined: 20 Jan 2012, 17:18
Contact:

Re: Resolving Binding for Dependency Property Returns Empty String

05 Jun 2025, 01:59

Thanks for your feedback!

Who is online

Users browsing this forum: No registered users and 5 guests