Ievgen
Ievgen

Reputation: 4443

Add items dynamically to ResourceDictionary. Convert File path to resource Key

I have ResourceDictionary in my application. I need to add some items from c# code to this collection:

<UserControl.Resources>
    <ResourceDictionary>
    </ResourceDictionary>
</UserControl.Resources>

As key for resources i want to use path to file. For example:

c:\some folder\@#file.txt

What is the best wey to convert this file path to valid ResourceDictionary Key?

Upvotes: 0

Views: 400

Answers (2)

Code0987
Code0987

Reputation: 2618

This article on CP tell you how use loose XAML files at Runtime, also some other. Have a look.

Upvotes: 1

Muhammad Hasan Khan
Muhammad Hasan Khan

Reputation: 35146

<UserControl.Resources>
    <ResourceDictionary xmlns:sys="clr-namespace:System;assembly=mscorlib">
        <sys:String x:Key="c&#x3A;some&#x20;folder&#x23;file.txt">
            whatever
        </sys:String>
    </ResourceDictionary>
</UserControl.Resources>    
<Grid>
    <Label Content="{StaticResource c&#x3A;some&#x20;folder&#x23;file.txt}" />
</Grid>

Remove back slash and encode special characters.

Upvotes: 1

Related Questions