Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data binding window title to application resource

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.

like image 657
alehro Avatar asked Mar 25 '11 13:03

alehro


People also ask

What is the data binding concept and how binding works in WPF?

Data binding is a mechanism in WPF applications that provides a simple and easy way for Windows Runtime apps to display and interact with data. In this mechanism, the management of data is entirely separated from the way data. Data binding allows the flow of data between UI elements and data object on user interface.

What is binding path in WPF?

Binding path syntax. Use the Path property to specify the source value you want to bind to: In the simplest case, the Path property value is the name of the property of the source object to use for the binding, such as Path=PropertyName . Subproperties of a property can be specified by a similar syntax as in C#.

What is XAML data binding?

Data binding is a mechanism in XAML applications that provides a simple and easy way for Windows Runtime Apps using partial classes to display and interact with data. The management of data is entirely separated from the way the data is displayed in this mechanism.


2 Answers

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.

like image 176
H.B. Avatar answered Sep 20 '22 14:09

H.B.


just remove this:

... ;assembly=ClassLibrary1"
like image 33
andre719mv Avatar answered Sep 23 '22 14:09

andre719mv