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:
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 orResources
needs to implementINotifyPropertyChanged
)If
Properties
is a namespace (as would be the case with the default VS-generated properties) you need to declare it somewhere usingxmlns
& usex:Static
: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 isinternal
which will throw an exception since binding only works for public properties.