Binding to a const field in Silverlight
Asked Answered
S

3

5

I have a situation where some application wide values are stored as constants - this is a requirement as they are needed in attribute definitions (attributes must resolve at compile time, so even static members don't work).

I wish to also be able to also reuse these values in XAML files. So if I have my constants like this:

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

I want to one way bind them to controls defined in XAML something like this:

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

Is this possible in a straight forward manner? I've looked over binding examples but can't seem to find this sort of scenario.

Would there maybe be some kind of work around I could do (maybe bindings translated into parameters for a method that dynamically pulls the constant field via reflection)

Sakai answered 21/12, 2009 at 17:55 Comment(0)
H
5

Here is the approach I would take:-

Through out the XAML I would use a StaticResource syntax like this:-

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

Create a static method somewhere that returns a ResourceDictionary and takes Type as parameter. The function uses reflection to enumerate the set of public constants it exposes. It adds the string value of each constant to the ResourceDictionary formulating the key name from the Type name and the Consts name.

During application startup pass typeof(MyConstants) to this function add the returned ResourceDictionaries to the collection in the Application Resources MergedDictionaries property.

Now all the static resources should resolve correctly, there is no need to invoke any binding or set any datacontext in order to get this to work. The value is resolved during XAML parsing.

Hercegovina answered 24/12, 2009 at 17:43 Comment(3)
This was exactly the kind of solution I was looking for, thank you.Sakai
@Anthony, can you paste the code here. I'm kind of confused in your description. thanks.Chitin
I also added an solution here, but the Visual Designer would not be reflected, though the runtime effect is correct.Chitin
Z
4

You can do this, but only if you implement a property that returns the constant. Binding only works against properties. To make this work, change your declaration to:

public class MyConstants
{
    private const string constant1 = "Hello World";
    public string Constant1 { get { return constant1; } }
}
Zygodactyl answered 21/12, 2009 at 17:59 Comment(2)
In order for this to work there needs to be an instance of a MyConstants class available in the DataContext.Hercegovina
Yes. You have to have something, somewhere, were you can get an instance. You could implement this property directly in your data context class, and pull the constant from there, as well.Zygodactyl
C
1

If you don't mind the Visual Designer, I would just do

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

Here:

  1. static and getter-only make it const,
  2. and it's also a property so that you can do binding like this.

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

However, as I said, it won't reflect on the Visual Designer (XAML) in Visual Studio, which is pretty big pity :-)

Chitin answered 2/1, 2012 at 1:15 Comment(1)
Is MyConstants a class? The syntax you have here currently won't compile. How does the Binding realise that the literal "MyConstants" in its path actually refers to a type rather than a property of the current DataContext? It would also need to understand that "Constant1" refers to static property on this type rather than an instance property. In my experience this doesn't work. What version of Silverlight are you using?Hercegovina

© 2022 - 2024 — McMap. All rights reserved.