Constant Field Linking in Silverlight

I have a situation where some application values ​​are widely used as constants - this is a requirement, since they are necessary in attribute definitions (attributes must be resolved at compile time, so even static members do not work).

I also want to be able to reuse these values ​​in XAML files. Therefore, if I have such constants:

public class MyConstants
{
   public const string Constant1 = "Hello World";
}

I want one way to associate them with controls defined in XAML as follows:

<TextBlock Text="{Binding MyConstants.Constant1}" />

Is it possible straightforward? I looked at the binding examples, but cannot find such a scenario.

Maybe there will be some work that I could do (maybe the bindings were translated into parameters for a method that dynamically pulls a constant field through reflection)

+5
3

, : -

XAML StaticResource : -

<TextBlock Text="{StaticResource MyConstants_Constant1}" />

-, ResourceDictionary Type . , . ResourceDictionary, Const.

typeof(MyConstants) ResourceDictionaries Application MergedDictionaries.

, - datacontext, . XAML.

+5

, , . . , :

public class MyConstants
{
    private const string constant1 = "Hello World";
    public string Constant1 { get { return constant1; } }
}
+4

Visual Designer,

public MyConstants
{
    public static string Constant1 { get { return "Hello World"; } }
}

:

  • static getter-only const,
  • , .

    <TextBlock Text="{Binding MyConstants.Constant1}" />

, , Visual Designer (XAML) Visual Studio, : -)

+1

All Articles