Home > Back-end >  How can I bind source of static resource in styles.xaml which is in Resources folders as Stringresou
How can I bind source of static resource in styles.xaml which is in Resources folders as Stringresou

Time:05-03

I tried to put static resource in styles.xaml which is in Resources folders as Stringresources are in. i had binded static resoure as same way before in other pages, and it worked well. and now exception coming up, and they say it can't find the source when i run this.

 <TextBlock 
    Text="{Binding Source={StaticResource ResourceKey=AllRooms}}" 
    TextWrapping="Wrap" FontWeight="Bold" FontSize="16" 
    FontFamily="Noto Sans CJK KR Regular" HorizontalAlignment="Right" 
    Margin="0,0,10,0" VerticalAlignment="Center" />
 <TextBlock HorizontalAlignment="Left" Margin="10,0,0,0" 
    FontSize="16" FontFamily="Noto Sans CJK KR Regular" 
    FontWeight="Bold"
    Text="{Binding Source={StaticResource ResourceKey=ByRoom}}" 
    TextWrapping="Wrap" VerticalAlignment="Center" />

CodePudding user response:

Your styles.xaml need to be included in App.xaml.

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Folder_name_if_exists/Styles.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

After this, your styles would be available via the StaticResource:

<TextBlock Text="{StaticResource AllRooms}"
           TextWrapping="Wrap"
           FontWeight="Bold" FontSize="16" 
           FontFamily="Noto Sans CJK KR Regular"
           HorizontalAlignment="Right" 
           Margin="0,0,10,0"
           VerticalAlignment="Center" />

In case you need to reference the *.resx file with your localization strings - you just need to include the containing namespace at the top of the file and then refer them via {x:Static}.

xmlns:res="clr-namespace:MyApp.NestedNamespace.Resources"
...

<TextBlock Text="{Binding Source={x:Static res.NameOfResourcesFile.NameOfResourceString}}"
           TextWrapping="Wrap"
           FontWeight="Bold" FontSize="16" 
           FontFamily="Noto Sans CJK KR Regular"
           HorizontalAlignment="Right" 
           Margin="0,0,10,0"
           VerticalAlignment="Center" />

UPDATE

Since your resources are static it is important to keep the declaration order from the most independent resources to the most dependent.

E.g.: In case you have a dictionary with colors and brushes and another dictionary with styles that use those colors - their declaration order should look like the following:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Folder_name_if_exists/BrushesAndColors.xaml"/>
            <ResourceDictionary Source="/Folder_name_if_exists/Styles.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

Declaring them in other ways will cause an exception.

  • Related