Page 1 of 1

Static Members Not Supported ?

Posted: 02 Jul 2024, 00:02
by samc
Hi,

I'm trying to bind a field to a constant in code.

Here is my xaml:
<MyUserControl MyBindingId="{x:Static local:Action.Id}">
And my C# code looks like this:
namespace Foo.Bar
{
  public static class Action
  {
    public const int Id = 42;
  }
}
When I do this, I'm getting an error at runtime:
Static members not supported. Can't use 'Foo.Bar.Action.Interact'
I'm confused about the error - is it saying the 'x:Static' part in my xaml is not allowed? Or is the problem that my class is static? The variable is a constant so I'm not sure if it's a mis-detection on that?

thanks,
sam

Re: Static Members Not Supported ?

Posted: 02 Jul 2024, 09:27
by sfernandez
Hi Sam,

It means that current x:Static implementation does not support accessing static members in classes (see here), it only works with enum values and a few more scenarios described in the documentation.

Perhaps you can use the DP default value option in this case, could that work for you?

Re: Static Members Not Supported ?

Posted: 02 Jul 2024, 23:20
by samc
Hmmm - My use case is that I'm trying to use code generated by Rewired (we are using it for input) to bind controls to specific actions in order to create a user interface so users can rebind controls. The code that Rewired generates uses constants rather than an enum value. I'll see if I can do something else on my end.

thanks,
sam

Re: Static Members Not Supported ?

Posted: 03 Jul 2024, 08:40
by sfernandez
I was referring to this case, I think it would work the same for const values.
public class SizeHelper : DependencyObject
{
  public static float BorderWidth { get { return 16.0f } };

#if NOESIS
  public static readonly DependencyProperty BorderWidthProperty = DependencyProperty.Register(
    "BorderWidth", typeof(float), typeof(SizeHelper),
    new PropertyMetadata(SizeHelper.BorderWidth));
#endif
}