tags:

views:

87

answers:

1

Here's my 'sample code', including what I'm trying to do. Obviously, it doesn't work at the moment, but is there any way I can make it work?

<ResourceDictionary 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"
    >
    <System:String x:Key="ProductName">Foo</System:String>
    <System:String x:Key="WindowTitle">{ProductName} + Main Window</System:String>
</ResourceDictionary>
+1  A: 

The only way to add a computed string to a ResourceDictionary in this way is to create a MarkupExtension. Your MarkupExtension would be used like this:

<ResourceDictionary ...>
  <sys:String x:Key="ProductName">Foo</sys:String>
  <local:MyStringFormatter
    x:Key="WindowTitle"
    StringFormat="{0} Main Window"
    Arg1="{StaticResource ProductName}" />
</ResourceDictionary>

This assumes you have created a MarkupExtension subclass in your "local" namespace named MyStringFormatterExtension that has properties named "StringFormat", "Arg1", "Arg2", etc, and has a ProvideValue() method that does the obvious.

Note that as Aran points out, a Binding using StringFormatter would be a more common way to achieve the same effect, and is generally the better design. The tradeoff is it would not allow the result to be used as part of a ResourceDictionary.

Ray Burns
Excellent - I hadn't come across either of these concepts in my search. Thanks!
Ant