2011-03-25 75 views
8

目前我正在做这样:数据窗口标题绑定到应用程序资源

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

如何通过WPF结合做?

编辑:它仍然无法在XAML中工作。
环境:VS2010,.NET 4.0,Windows 7的
繁殖步骤:
的代码创建类库ClassLibrary1的:

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

在VS2010 .NET 4.0中创建WPF Windows应用程序。
编辑主窗口的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}"> 

...

编译错误信息:
MainWindow.xaml(7,130):错误MC3029: 'ClassLibrary1的:1类' 成员无效,因为它没有合格的类型名称。

我还发现此主题My.Resources in WPF XAML?。 而且它似乎都应该工作,但它不。

Microsoft不对此错误消息给出说明。只有帮助论坛http://social.msdn.microsoft.com/Forums/en/wpf/thread/4fe7d58d-785f-434c-bef3-31bd9e400691中的另一个主题,这也没有帮助。

+0

在这种情况下的路径应该是'X里面:Static'的属性是静态的,即'{绑定源= {x:Static ClassLibrary1:Class1.Something}}',请参阅[参考页](http://msdn.microsoft.com/zh-cn/library/ms742135.aspx)上的语法。我也更新了我的答案,这是错误的... – 2011-09-24 15:48:25

回答

0

其实,我在应用程序的顶部定义的静态资源的标题,我绑定标题和其他任何我想它

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

在代码中它是这样的,我认为:

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

这仅改变可能发生的称号,并绑定将通知这些变化是有道理的(WindowName必须或者是一个依赖属性或Resources需要实现INotifyPropertyChanged

如果Properties是一个命名空间(如将使用默认VS-生成的属性的情况下),你需要声明它使用的地方xmlns &使用x:Static

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

另注:如果您使用管理您需要确保属性的访问修饰符为public,默认值为internal,这将引发异常,因为绑定仅适用于公共属性。

+0

谢谢。你最后的建议是我的情况,但它不起作用。请参阅我的文章的编辑:。我已经清楚地说明了这一点:将课程移到不同的程序集中,等等。 – alehro 2011-03-26 06:39:24

+0

我编辑了我的答案,以提供更多建议,希望其中一些适合您的问题。 – 2011-03-26 12:23:08

+0

您首先列出的方法背后的代码不起作用。您不能将“Properties.Resources”设置为绑定源。 “...资源是一种类型,在给定的上下文中无效”我尝试在那里指定资源键,并且它接受它,但绑定失败。文本没有显示任何内容 – 2016-03-02 20:43:50

0

您是否尝试将资源的访问修饰符从内部更改为公共?

我刚才有一些问题,现在。

/// <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); 
     } 
    } 

/// <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); 
     } 
    } 
+1

是的,我已经完成了。请看我的问题的ClassLibrary1 的源代码。有所有公众。 – alehro 2011-08-01 17:24:26

1

只是删除此:

... ;assembly=ClassLibrary1" 
相关问题