开发者

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.


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.


just remove this:

... ;assembly=ClassLibrary1"


I actually have the Title in a static resource defined at the top of the application and I bind the Title and anything else I want to it

<s:String x:Key="ApplicationName">My Application</s:String>


Have you tried to change the access modifier of the resource from internal to public?

I have just had some problem with that right now.

    /// <summary>
    ///   Looks up a localized string similar to Has been impossible to load the configuration information.
    /// </summary>
    internal static string ERROR_NoConfigurationLoaded {
        get {
            return ResourceManager.GetString("ERROR_NoConfigurationLoaded", resourceCulture);
        }
    }

to

    /// <summary>
    ///   Looks up a localized string similar to Has been impossible to load the configuration information.
    /// </summary>
    public static string ERROR_NoConfigurationLoaded {
        get {
            return ResourceManager.GetString("ERROR_NoConfigurationLoaded", resourceCulture);
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜