【发布时间】: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