1

I have this xaml

<mui:ModernWindow x:Uid="mui:ModernWindow_1" x:Class="App1.HomePage"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mui="http://firstfloorsoftware.com/ModernUI"
  xmlns:sys="clr-namespace:System;assembly=mscorlib"
  ContentSource="/Window1.xaml"
  Title="MainWindow" Height="350" Width="525" WindowState="Maximized" MenuLinkGroups="{Binding menuLinkGroups}">
    <mui:ModernWindow.Resources>
        <sys:String x:Key="ApplicationName">Bla Bla</sys:String>
    </mui:ModernWindow.Resources>
    <Grid Style="{StaticResource ContentRoot}" Name="mainGrid">
    </Grid>
</mui:ModernWindow>

I need to reference current window resources, so I used this:

object obj = this.Resources["ApplicationName"];

But this.Resources doesn't have any resource! so obj is always null. How could I reference this window resources?

Marcel B
  • 3,624
  • 2
  • 27
  • 39
user3222589
  • 184
  • 1
  • 13

5 Answers5

0

Assume that this is a FrameworkElement, like a Window, a Grid, a Button or something like that.

object obj = this.TryFindResource("ApplicationName");
Marcel B
  • 3,624
  • 2
  • 27
  • 39
  • hmm… strange… I saw on Codeplex that ModernWindow inherits from Window (which inherits from `FrameworkElement`) and that neither Resources nor TryFindResource is overriden. – Marcel B Jul 23 '14 at 13:35
0

you can use below mentioned code

var MainGrid=Application.Current.FindResource("strApp")

or

this.FindResource("ApplicationName")
Dhaval Patel
  • 7,471
  • 6
  • 37
  • 70
0

Assuming this is a control...

var parent = Window.GetWindow(this)

Will get the window the control is currently on, you should then be able to access the resources like you already did

parent.Resources["ApplicationName"];
Greg B
  • 416
  • 3
  • 12
0

Thanks all, I find the solution (I forget to update the localized dll).

I cleaned and rebuilt solution, used locbaml.exe again to generate new localized dll.

user3222589
  • 184
  • 1
  • 13
0

You should bind in the XAML and not in the code behind.

"res" is the namespace where the resources file is located.

In your example the namespace alias is "local":

xmlns:local="clr-namespace:Project.Resources"

So your code should look like:

<Page Title ="{x:Static local:localStrings.step1Description}" />

Where:

"local" is the namespace alias where the resources file is located.

"localStrings" is the name of the resource file.

"step1Description" is an entry in the resource file.

aydjay
  • 858
  • 11
  • 25
  • In my case, I'm handling this in code behind to generate a user-specific menu, menu strings are stored as resources to give multilingual support, if you have a better approach to bind directly please point me to. – user3222589 Jul 24 '14 at 07:09