Resolving Binding for Dependency Property Returns Empty String
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 -->
-
-
sfernandez
Site Admin
- Posts: 3363
- Joined:
Re: Resolving Binding for Dependency Property Returns Empty String
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. Hope this helps.
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. Hope this helps.
Re: Resolving Binding for Dependency Property Returns Empty String
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:
Hopefully this helps someone in the future!
For reference:
The correct binding is actually:
Code: Select all
Action="{Binding DataContext.MyActionName, ElementName=MyGrid}" For reference:
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
}
public InputActionGlyph()
{
InitializeComponent();
DataContext = new InputActionGlyphModel();
// ...
}
// ...
}
Re: Resolving Binding for Dependency Property Returns Empty String
Thanks for your feedback!
Who is online
Users browsing this forum: No registered users and 5 guests