C# – Data binding window title to application resource

cdata-bindingwpfxaml

Currently I'm doing it so:

    public MainWindow()
    {
        InitializeComponent();
        Title = Properties.Resources.WindowName;
    }

How to do the same through the WPF binding?

EDIT: It still doesn't work in XAML.
Environment:VS2010, .NET 4.0, Windows 7.
Reproduction steps:
Create class library ClassLibrary1 with code:

 namespace ClassLibrary1
 {
    static public class Class1
    {
        static public string Something
        {
            get { return "something"; }
        }
    }
}

Create WPF windows application in VS2010 .NET 4.0.
Edit main window's XAML:

<Window x:Class="ahtranslator.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        
    xmlns:ClassLibrary1="clr-namespace:ClassLibrary1;assembly=ClassLibrary1" 
    Title="{Binding Source={x:Static ClassLibrary1:Class1}, Path=Something}"
    Height="350" Width="525" Icon="/ahtranslator;component/Icon1.ico"    WindowStyle="SingleBorderWindow" ShowInTaskbar="False" DataContext="{Binding}">

Compilation error message:
MainWindow.xaml(7,130): error MC3029: 'ClassLibrary1:Class1' member is not valid because it does not have a qualifying type name.

Also I found this topic My.Resources in WPF XAML?.
And it seems all should work but it doesn't.

Microsoft doesn't give description for this error message. Only another topic in help forum http://social.msdn.microsoft.com/Forums/en/wpf/thread/4fe7d58d-785f-434c-bef3-31bd9e400691, which doesn't help either.

Best Answer

In code it would look like this i think:

Binding titleBinding = new Binding("WindowName");
titleBinding.Source = Properties.Resources;
this.SetBinding(Window.Title, titleBinding);

This only makes sense if changes may occur to the title and the binding will be notified of those changes (WindowName has to either be a Dependency Property or Resources needs to implement INotifyPropertyChanged)

If Properties is a namespace (as would be the case with the default VS-generated properties) you need to declare it somewhere using xmlns & use x:Static:

<Window
   ...
   xmlns:prop="clr-namespace:App.Properties"
   Title="{Binding Source={x:Static prop:Resources.WindowName}}">

Another note: If you use the managed resources of Visual Studio you need to make sure that the access modifier of the properties is public, default is internal which will throw an exception since binding only works for public properties.