简体   繁体   中英

How can I bind source of static resource in styles.xaml which is in Resources folders as Stringresources are in?

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" />

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.

Eg: 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM