Creating a Resources.resx file

1.) Right Click the Project select Add New Item -> General -> Resource File. and provide a name.



2.) Set the Access Modifier of the MyResources.resx to Public. Make it Public allows us to bind in XAML.



XAML Binding:

3.) Add the Namespace of the Resources.resx in XAML




xmlns:local="clr-namespace:WpfApplication12"

4.) Binding Syntax: ResourceFileNamespace.ResourceFile.ResourceKey

ResourceFileNamespace - WpfApplication12
ResourceFile - MyResources

 Syntax: WpfApplication12.MyResources.ResourceKey

We have already referenced WpfApplication12 as local in XAML

 Syntax: local:MyResources.ResourceKey


<StackPanel Orientation="Vertical">
            <TextBlock Text="{x:Static local:MyResources.MyName}" FontSize="20"/>
            <TextBlock Text="{x:Static local:MyResources.MyId}" FontSize="20"/>
            <TextBlock Text="{x:Static local:MyResources.MyAge}" FontSize="20"/>
 </StackPanel>


Whole XAML:

<Window
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     
        x:Class="WpfApplication12.MainWindow"
        xmlns:local="clr-namespace:WpfApplication12"      
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style TargetType="TextBlock">
            <Setter Property="FontSize" Value="20"/>
        </Style>
    </Window.Resources>
    <Grid>
        <StackPanel Orientation="Vertical">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Name : "/>
                <TextBlock Text="{x:Static local:MyResources.MyName}"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Id : "/>
                <TextBlock Text="{x:Static local:MyResources.MyId}"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Age : "/>
                <TextBlock Text="{x:Static local:MyResources.MyAge}"/>
            </StackPanel>
        </StackPanel>
    </Grid>
</Window>


Output: