I once had a WinForms application in which I used a static class for simple string resources. In this class I had constant strings I could acces. One of those strings consisted of the value of another constant, plus its own value. Something like this:
private const string Path = @"C:\SomeFolder\";
public const string FileOne = Path + "FileOne.txt";
public const string FileTwo = Path + "FileTwo.txt";
Now I have a WPF application and I am using a ResourceDictionary, which I merged to Application scope. Everything works fine, but I want something similar like the C# code above. This is what I already have:
<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="Path">C:\SomeFolder\</System:String>
<System:String x:Key="FileOne">FileOne.txt</System:String>
<System:String x:Key="FileTwo">FileTwo.txt</System:String>
</ResourceDictionary>
Now I need something (some kind of reference to ‘Path’) that is automatically added to the two file-strings, it doesn’t need to be private like in the C# code. Does anyone know how I can achieve this?
Thanks in advance!
If you want to use it i XAML, here are two ideas for you:
Idea 1: one
BindingIn your resources, only add the
Path:And, somewhere in your XAML:
This will display Path+”FileOne.txt”
(note: you can write whatever you want instead of FileOne.txt )
You can have custom stuff using this way.
Idea 2: One
MultiBindingMore convenient for what you are trying to do imho: you keep those three
Resourcesyou defined.If you want to call them somewhere so that Path + fileOne will be displayed, just use that (example with a
TextBlock)That’s all you need!
Alternatively if you don’t use those
Strings in your UI, you can still use a static class. But using the XAML way is cleaner imho (all UI-related stuff should stay in XAML anyway)