24

Is there a way to embed a string in XAML, give it and ID and refer to it later.

I have tried:

    <Window x:Class="WpfApp1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:System="clr-namespace:System;assembly=mscorlib"
        Title="Window1" Height="300" Width="500">
        <Grid>
            <System:String>Test</System:String>
        </Grid>
    </Window>

And get error:
Cannot add instance of type 'String' to a collection of type 'UIElementCollection'. Only items of type 'UIElement' are allowed.

Could I do this if I nested the String somewhere else in the XAML? or inside a non UI element? Then do I just give it a Name attribute?

5 Answers 5

43

You should use Window.Resources

Here's an example for Page, in your case it will be Window.Resources tag:

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:System="clr-namespace:System;assembly=mscorlib">
  <Page.Resources>
    <System:String x:Key="MyString">Hello</System:String>
  </Page.Resources>
  <Grid>  
    <TextBlock Text="{StaticResource MyString}"></TextBlock>
  </Grid>
</Page>
3
  • And as long as the string is in there it will be fine?
    – BuddyJoe
    Commented Nov 30, 2009 at 17:41
  • 3
    Yes. Resources dictionaries can hold objects of any type, and you can reference it using {StaticResource} via its x:Key later. Commented Nov 30, 2009 at 17:44
  • Awesome! I tested pulling this value from C# code and it worked great too. thanks +1 and answer
    – BuddyJoe
    Commented Nov 30, 2009 at 17:50
9

In the Application tag you need to include the following:

xmlns:system="clr-namespace:System;assembly=mscorlib">

without the above code, Visual Studio will complain about a missing assembly reference.

2
  • Sorry, ignore this, I see u had already included it. the above posts should solve ur problem.
    – FruityMo
    Commented Dec 9, 2011 at 8:18
  • 4
    If you think your own answer wasn't useful, you can delete it by using the delete link under the answer. Commented Dec 9, 2011 at 8:20
5

I don't know why, but in my .Net Core 3 WPF app I should use this xmlns definition instead of "mscorlib":

xmlns:system="clr-namespace:System;assembly=System.Runtime"

then I can define:

<system:Double x:Key="FontSizeLarge">24</system:Double>

or

<system:String x:Key="StringTest">Test</system:String>
2

Having a reference to the string will not allow you to change it later, since strings are immutable, so as Yacoder suggests, just put it in the <Window.Resources> section. Something like:

<Window.Resources>
        <System:String x:Key="TestString">Test</System:String>
</Window.Resources>

If you need to be able to change the value of the string that appears in your grid, you'll want to use a TextBlock or other control whose Content property can be set.

1

And if you're like me and have typed an unwanted extra character somewhere in the XAML file, you can get this error. Fortunately I had GIT watching over my shoulder, so "Compare with Unmodified" quickly revealed that character that I had mistakenly typed at a place. Hope this can save some hair for you. :)

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