【问题标题】:Data binding window title to application resource数据绑定窗口标题到应用程序资源
【发布时间】:2012-01-13 08:53:51
【问题描述】:

目前我正在这样做:

    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:Class1”成员无效,因为它没有限定类型名称。

我也发现了这个话题My.Resources in WPF XAML?。 似乎一切都应该起作用,但事实并非如此。

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

【问题讨论】:

  • 这种情况下的路径应该在x:Static 内,因为属性是静态的,即{Binding Source={x:Static ClassLibrary1:Class1.Something}},请参见reference page 上的语法。我还更新了被误导的答案...

标签: c# wpf xaml data-binding


【解决方案1】:

我认为在代码中它看起来像这样:

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}}">

另一个注意事项:如果您使用 Visual Studio 的托管资源,您需要确保属性的访问修饰符是public,默认为internal,这将引发异常,因为绑定仅适用于公共属性。

【讨论】:

  • 谢谢。你的最后一个建议是我的情况,但它不起作用。请参阅编辑:我的帖子。我已经说得非常清楚了:将课程移至不同的程序集等等。
  • 我编辑了我的答案以提供更多建议,希望其中一些适合您的问题。
  • 您首先列出的方法背后的代码不起作用。您不能将Properties.Resources 设置为绑定源。 “...Resources 是一种类型,在给定的上下文中无效”我尝试在那里指定资源键,它接受它,但绑定失败。文字不显示任何内容
  • (更新)所以我发现自己处于同样的情况并偶然发现了同样的问题......这次绑定有效,但没有更新(指定资源键时) - 我仍然可以'不将 Properties.Resources 设置为绑定源,并收到相同的错误。
【解决方案2】:

删除这个:

... ;assembly=ClassLibrary1"

【讨论】:

    【解决方案3】:

    我实际上在应用程序顶部定义的静态资源中拥有标题,我将标题和我想要的任何其他内容绑定到它

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

    【讨论】:

      【解决方案4】:

      您是否尝试过将资源的访问修饰符从 internal 更改为 public?

      我现在遇到了一些问题。

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

      【讨论】:

      • 是的,我做到了。请查看我的问题的 ClassLibrary1 的源代码。都是公开的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多