ale*_*hro 8 c# data-binding wpf xaml
目前我这样做:
public MainWindow()
{
InitializeComponent();
Title = Properties.Resources.WindowName;
}
Run Code Online (Sandbox Code Playgroud)
如何通过WPF绑定做同样的事情?
编辑:它仍然无法在XAML中工作.
环境:VS2010,.NET 4.0,Windows 7.
复制步骤:
使用代码创建类库ClassLibrary1:
namespace ClassLibrary1
{
static public class Class1
{
static public string Something
{
get { return "something"; }
}
}
}
Run Code Online (Sandbox Code Playgroud)
在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}">
Run Code Online (Sandbox Code Playgroud)
...
编译错误消息:
MainWindow.xaml(7,130):错误MC3029:'ClassLibrary1:Class1'成员无效,因为它没有合格的类型名称.
另外我在WPF XAML中发现了这个话题My.Resources?.似乎一切都应该有效,但事实并非如此.
Microsoft未提供此错误消息的说明.帮助论坛http://social.msdn.microsoft.com/Forums/en/wpf/thread/4fe7d58d-785f-434c-bef3-31bd9e400691中只有另一个主题,这也无济于事.
H.B*_*.B. 10
在代码中它看起来像我认为:
Binding titleBinding = new Binding("WindowName");
titleBinding.Source = Properties.Resources;
this.SetBinding(Window.Title, titleBinding);
Run Code Online (Sandbox Code Playgroud)
只有在标题发生更改并且绑定将被通知这些更改(WindowName
必须是依赖属性或Resources
需要实现INotifyPropertyChanged
)时才有意义
如果Properties
是命名空间(就像默认的VS生成的属性一样),你需要使用xmlns
&use 在某处声明它x:Static
:
<Window
...
xmlns:prop="clr-namespace:App.Properties"
Title="{Binding Source={x:Static prop:Resources.WindowName}}">
Run Code Online (Sandbox Code Playgroud)
另一个注意事项:如果使用Visual Studio的托管资源,则需要确保属性的访问修饰符是public
,default internal
将抛出异常,因为绑定仅适用于公共属性.
Rac*_*hel -1
实际上,我在应用程序顶部定义的静态资源中拥有标题,并且我将标题和我想要的任何其他内容绑定到它
<s:String x:Key="ApplicationName">My Application</s:String>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
8157 次 |
最近记录: |