将 WPF 窗口标题绑定到 shell 视图模型类中的属性时出现问题 [Caliburn.Micro]
我在 WPF 窗口的 Title 属性上的 shell 视图模型类中绑定属性时遇到简单的问题 - 它是 shell。
我的 shell 视图如下所示:
<Window x:Class="Spirit.Views.ShellView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="{Binding Path=Title}" >
<Grid>
<ContentControl x:Name="ActiveItem" />
</Grid>
</Window>
shell 视图模型类:
[Export(typeof(IShellViewModel))]
public class ShellViewModel : Conductor<IScreen>.Collection.OneActive, IShellViewModel
{
private string _title;
public string Title
{
get { return _title; }
set
{
_title = value;
NotifyOfPropertyChange(()=>Title);
}
}
public ShellViewModel()
{
Title = "Spirit";
}
}
如果我运行应用程序 shell 视图(WPF 窗口)的标题是 Namespace.ShellViewModelClass,则 shell 视图模型类中没有属性 Title 的值。
如果我在 shell 视图中激活某个屏幕,窗口的 Title 属性是 Namespace.ViewModelClass。
我怎样才能消除这种行为?感谢您的建议。
I have simple problem with binding property in shell view model class on Title property of WPF Window- it’s shell.
My shell view look like this:
<Window x:Class="Spirit.Views.ShellView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="{Binding Path=Title}" >
<Grid>
<ContentControl x:Name="ActiveItem" />
</Grid>
</Window>
shell view model class:
[Export(typeof(IShellViewModel))]
public class ShellViewModel : Conductor<IScreen>.Collection.OneActive, IShellViewModel
{
private string _title;
public string Title
{
get { return _title; }
set
{
_title = value;
NotifyOfPropertyChange(()=>Title);
}
}
public ShellViewModel()
{
Title = "Spirit";
}
}
If I run app Title of shell view (WPF window) is Namespace.ShellViewModelClass, no value of property Title in shell view model class.
If I active some screen in shell view, Title property of window is Namespace.ViewModelClass.
How can I remove this behavior? Thank for advice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于 IScreen 是使用 IHaveDisplayName 定义的,并且 CM 框架的 Screen 类具有 DisplayName 属性,因此您只需在 ShellViewModel 中设置该属性,如下所示:
Since IScreen is defined with IHaveDisplayName and the CM framework's Screen class has a property of DisplayName, you just need to set that property in your ShellViewModel, like this:
从您给出的代码中很难看出,但我假设您将 Window 的 DataContext 分配给代码隐藏中的 ShellViewModel 实例。 ShellViewModel 何时初始化?
您需要在 ViewModel 中为任何属性实现 INotifyPropertyChanged您想要查看其更改后的值。这里的链接是 MSDN 文档,但是如果您在 Google 和/或 SO 中搜索它,您将看到大量示例。
It's a little difficult to tell from the code you've given, but I assume that you assign the DataContext of your Window to an instance of ShellViewModel in your code-behind. When is the ShellViewModel initialized?
You need to implement INotifyPropertyChanged in your ViewModel for any properties that you want to see a changed value for. The link here is to the MSDN documentation, but if you search Google and/or SO for it, you will see plenty of examples.