8

我在 WPF Window 的 Title 属性上的 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>

外壳视图模型类:

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

如果我运行外壳视图(WPF 窗口)的应用程序标题为 Namespace.ShellViewModelClass,则外壳视图模型类中没有属性标题的值。

如果我在 shell 视图中激活某个屏幕,则窗口的 Title 属性是 Namespace.ViewModelClass。

我怎样才能消除这种行为?感谢您的建议。

4

2 回答 2

20

由于 IScreen 是使用 IHaveDisplayName 定义的,并且 CM 框架的 Screen 类具有 DisplayName 属性,因此您只需在 ShellViewModel 中设置该属性,如下所示:

public ShellViewModel()
{
    base.DisplayName = "Spirit";
}
于 2011-01-06T21:01:31.883 回答
0

从您提供的代码中很难分辨,但我假设您将 Window 的 DataContext 分配给代码隐藏中的 ShellViewModel 实例。ShellViewModel 什么时候初始化?

您需要在 ViewModel 中为要查看更改值的任何属性实现INotifyPropertyChanged 。这里的链接指向 MSDN 文档,但是如果您在 Google 和/或 SO 中搜索它,您会看到很多示例。

于 2011-01-06T13:56:16.727 回答