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

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.