30

IS there a way to define a constant string to be used as a static resource across the whole application?

I am running a Wpf application but there is no main xaml form. The application is a collection of xaml controls handled by a single classic .cs form.

4 Answers 4

90

You can define it as an application resource:

 <Application x:Class="xxxxxx"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:clr="clr-namespace:System;assembly=mscorlib"
                 StartupUri="MainWindow.xaml">
        <Application.Resources>
            <clr:String x:Key="MyConstString">My string</clr:String>
        </Application.Resources>
    </Application>
5
  • We have different approaches there, I'd like to know if you have an opinion on mine as well? I usually put all my resources into separate dictionary files for more readability (putting all the stuff in Application.Resources may finish on an incredible messy code), but does that include a difference in performance?
    – Damascus
    Apr 14, 2011 at 9:57
  • 1
    @Damascus: maybe having a separate dictionary is a better approach. I don't think there is a sensible performance difference, but never tryied a comparison: may be is a tiny optimization to ignore. Apr 14, 2011 at 10:05
  • this would be ideal but as i said i dont have an application.xaml only a collection of usercontols so this wouldnt work in my case
    – Marcom
    Apr 14, 2011 at 10:06
  • @Marcom ok, so probably the Damascus reply is the one for you Apr 14, 2011 at 10:06
  • 4
    Note that you can use <x:Static Member="clr:String.Empty" /> to declare an empty string
    – Yogu
    Jul 28, 2013 at 19:57
15

Supplemantary to the answer by @FelicePollano - for code indentation to work I put this as a separate 'answer'.

If you happen to have your original constant defined in a .cs-file you can avoid duplicating its value in <Application.Resources> by this:

<x:Static x:Key="MyConstString" Member="local:Constants.MyString" />

For the reference local above to work you need to include the namespace xmlns:local="clr-namespace:Utils" in the tag <Application>.

The cs-class could then look like this:

namespace Utils 
{
    public class Constants
    {
        public const string MyString = "My string";
    }
}

An example on usage in the xaml-code could then be:

<TextBlock Text="{StaticResource MyConstString}" />
3
  • 1
    I like this the best. It allows a single definition for a string that can be consumed from both XAML and C#, without the XAML needing to know about the code (outside the resource definition).
    – dlf
    Feb 17, 2016 at 22:05
  • 1
    Double quotes are missing. <x:Static x:Key="MyConstString" Member="local:Constants.MyString" />
    – sa.he
    Dec 20, 2018 at 7:22
  • With this, I get this error: An object of the type "System.Windows.Markup.StaticExtension" cannot be applied to a property that expects the type "System.String". Does this solution really work? Sep 19, 2022 at 17:49
7

Just add a resource dictionary XAML file, let's say it's named Dictionary.xaml (Visual Studio can create you one automatically)

Then, add your static resource in this dictionary.

To finish, reference the dictionary in all your XAML controls:

<UserControl.Resources>
                <ResourceDictionary Source="Dictionary.xaml"/>
    </UserControl.Resources>
2
  • It works nicely although it can be a bit cumbersome if you have a lot of contols
    – Marcom
    Apr 14, 2011 at 10:07
  • I think that even if you have a lot of controls, it won't be that heavy: it's three lines to add for each control of your solution, and it especially helps you to understand your code more easily (as an example I have many different dictionaries in my solution: one for converters, one for templates, one for animations... Helps me getting to what I want very easily). But of course if you've only one resource to keep shared, it will feel heavy
    – Damascus
    Apr 14, 2011 at 10:15
5

You can use like this:

First, sample constant variable:

namespace Constants
{
    public class ControlNames
    {
        public const string WrapperGridName = "WrapperGrid";
    }
}

And second XAML using:

<TextBlock Text="{x:Static Member=Constants:ControlNames.WrapperGridName}"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.