Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fetch string from resource to assign in WPF Resource section in xaml

I have a XBAP application with the following user control:

  <UserControl x:Class="XXX.UsersGrid"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Height="Auto" Width="Auto">

        <UserControl.Resources>
            <DataTemplate x:Key="UpArrowUsers">
                <DockPanel>
                    <TextBlock Text="xxUser" x:Name="upArrowUsersHeader" HorizontalAlignment="Center"></TextBlock>
                    <Path x:Name="arrow" StrokeThickness = "1" Fill= "gray" Data= "M 5,10 L 15,10 L 10,5 L 5,10"/>
                </DockPanel>
            </DataTemplate>
    </UserControl>
    ...

Now I want to fetch the string "xxUser" from a resx file which is embed as resource in the application How do I achieve this?

like image 337
coder_bro Avatar asked Mar 20 '09 11:03

coder_bro


1 Answers

Create a static class that makes the resources available as properties:

public static class Resources
{
   public string Resource
   {
      return Properties.Resources.ResourceManager.GetString("Resource");
   }
}

Then you can bind your TextBox to this:

<TextBlock Text="{Binding Source={x:Static local:Resources}, Path=Resource}" x:Name="upArrowUsersHeader" HorizontalAlignment="Center"
   xmlns:local="clr-namespace:MY_NAMESPACE;assembly=MY_ASSEMBLY">
like image 152
Josh G Avatar answered Oct 12 '22 12:10

Josh G