Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wpf window title from static resource

I am using resource dictionarys for localizations, I have this code in wpf:

<Window x:Class="RWIS_WPF.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="RWIS" Height="500" Width="800" MinHeight="500" MinWidth="800">

    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Localizations/Dictionary.EN.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>

My problem is, that I want to localize window title using {StaticResource mW_screen1}

    <Window x:Class="RWIS_WPF.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

            Title="{StaticResource IT_IS_NOT_WORKING}" Height="500" Width="800"
            MinHeight="500" MinWidth="800">

            <Window.Resources>
              <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="Localizations/Dictionary.EN.xaml" />
                </ResourceDictionary.MergedDictionaries>
             </ResourceDictionary>
            </Window.Resources>
            <TextBlock Text="{StaticResource IT_IS_WORKING}"></TextBlock>

But it is not working, because resource is defined after title is calling it. It will give me error when I try to run it

System.Windows.Markup.XamlParseException occurred Message='Provide value on 'System.Windows.StaticResourceExtension' threw an exception.' Line number '6' and line position '9'.

It is working for headers, text after adding resource

I have tried to call it in c# code, but i was not succesful.
I know there is option:

<Window.Title></Window.Title>

but there is no argument like text or value, where can i put Text="{StaticResource IT_IS_WORKING}"

like image 256
Tomas Blaho Avatar asked Nov 07 '13 15:11

Tomas Blaho


2 Answers

StaticResource are applied at the time of loading of BAML (compiled XAML) into memory and it parses XAML from top to bottom and since your resource haven't created yet, it throws error while loading of XAML.

Instead, try using DynamicResource which is lazy loaded version you can say. It assigns an expression object to target property. This defers looking up the resource until it is needed at runtime.

Read this for further clarification - StaticResource V/S DyanamicResource.

<Window Title="{DynamicResource IT_WILL_WORK}"/>
like image 144
Rohit Vats Avatar answered Nov 15 '22 07:11

Rohit Vats


Just use the more verbose definition of StaticResource:

xmlns:System="clr-namespace:System;assembly=mscorlib"

...

<Window.Resources>
    <System:String x:Key="Title">Some Title</System:String>
    ...
</Window.Resources>
<Window.Title>
    <StaticResource ResourceKey="Title" />
</Window.Title>
like image 40
Sheridan Avatar answered Nov 15 '22 08:11

Sheridan