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.

Marcom
  • 4,621
  • 8
  • 54
  • 78

4 Answers4

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>
Felice Pollano
  • 32,832
  • 9
  • 75
  • 115
  • 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 '11 at 09: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. – Felice Pollano Apr 14 '11 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 '11 at 10:06
  • @Marcom ok, so probably the Damascus reply is the one for you – Felice Pollano Apr 14 '11 at 10:06
  • 4
    Note that you can use [``](http://stackoverflow.com/questions/7203996/how-to-declare-an-empty-string-in-xaml-resourcedictionary) to declare an empty string – Yogu Jul 28 '13 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}" />
Daniel Möller
  • 84,878
  • 18
  • 192
  • 214
Henrik
  • 1,897
  • 1
  • 16
  • 9
  • 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 '16 at 22:05
  • 1
    Double quotes are missing. – sa.he Dec 20 '18 at 07: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? – Daniel Möller Sep 19 '22 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>
Damascus
  • 6,553
  • 5
  • 39
  • 53
  • It works nicely although it can be a bit cumbersome if you have a lot of contols – Marcom Apr 14 '11 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 '11 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}"
bafsar
  • 1,080
  • 2
  • 16
  • 16