Reputation: 14432
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!
Upvotes: 0
Views: 680
Reputation: 6651
If you want to use it i XAML, here are two ideas for you:
Idea 1: one Binding
In your resources, only add the Path
:
<System:String x:Key="Path">C:\SomeFolder\</System:String>
And, somewhere in your XAML:
<TextBlock Text='{Binding Source={StaticResource Path}, StringFormat={}{0}FileOne.txt}' />
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 MultiBinding
More convenient for what you are trying to do imho: you keep those three Resources
you defined.
If you want to call them somewhere so that Path + fileOne will be displayed, just use that (example with a TextBlock
)
<TextBlock >
<TextBlock.Text>
<MultiBinding StringFormat="{}{0}{1}">
<Binding Source="{StaticResource Path}" />
<Binding Source="{StaticResource FileOne}" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
That's all you need!
Alternatively if you don't use those String
s 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)
Upvotes: 1
Reputation: 1892
You can still use a static class with resources:
namespace WpfStaticResources
{
class MyResources {
private const string Path = @"C:\SomeFolder\";
public const string FileOne = Path + "FileOne.txt";
public const string FileTwo = Path + "FileTwo.txt";
}
}
and then from your XAML reference that as:
<Window x:Class="WpfStaticResources.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:WpfStaticResources="clr-namespace:WpfStaticResources"
Title="{x:Static WpfStaticResources:MyResources.FileOne}" Height="350" Width="525">
<Grid>
</Grid>
</Window>
Personally I'd prefer to not use a static class, but instead create a domain object, set it as your DataContext and then bind to properties on it instead.
Upvotes: 1