WPF将窗口标题绑定到ViewModel属性

时间:2013-06-05 16:30:01

标签: c# wpf data-binding mvvm

我正在尝试将Window Title绑定到具有Title属性的ViewModel。以下是MainWindow XAML:

<Window x:Class="MyProject.View.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="clr-namespace:MyProject.ViewModel;assembly=MyProject.ViewModel"
        Title="{Binding Path=Title}" Height="350" Width="525" DataContext="{Binding Source={StaticResource mainWindowViewModel}}">
    <Window.Resources>
        <vm:MainWindow x:Key="mainWindowViewModel"/>
    </Window.Resources>

...

</Window>

当我尝试运行它时,我得到以下异常“在'System.Windows.StaticResourceExtension'上提供值引发异常。行号和位置指向DataContext属性,内部异常指出”无法找到资源名为mainWindowViewModel。

以下是视图模型的代码:

namespace MyProject.ViewModel
{
    public class MainWindow : INotifyPropertyChanged
    {
        #region Fields

        private const string TitlebarPrefixString = "My Project";
        private string title = TitlebarPrefixString;

        public string Title {
            get
            {
                return this.title;
            } // End getter
            set
            {    
                this.title = value;
                OnPropertyChanged("Title");
            } // End setter
        } // End property

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            } // End if
        } // End method


        public event PropertyChangedEventHandler PropertyChanged;

    } // End class
} // End namespace

我的理论是,在尝试将标题绑定到属性之后加载资源。抛出异常时,Window的Resources属性为空。

在Code Behind中设置DataContext的唯一解决方案是什么?我可以让它工作,但我更愿意将它保留在XAML中。

2 个答案:

答案 0 :(得分:12)

您可以尝试使用属性元素语法设置DataContext

<Window x:Class="MyProject.View.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:vm="clr-namespace:MyProject.ViewModel;assembly=MyProject.ViewModel"
    Title="{Binding Path=Title}" Height="350" Width="525">
<Window.Resources>
    <vm:MainWindow x:Key="mainWindowViewModel"/>
</Window.Resources>
<Window.DataContext>
  <StaticResourceExtension ResourceKey="mainWindowViewModel"/>
</Window.DataContext>

这应该起作用,因为xaml解析器将在设置资源字典后执行StaticResourceExtension

但我认为可能更好的是直接设置DataContext,而不将其声明为资源:

<Window x:Class="MyProject.View.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:vm="clr-namespace:MyProject.ViewModel;assembly=MyProject.ViewModel"
    Title="{Binding Path=Title}" Height="350" Width="525">
<Window.DataContext>
    <vm:MainWindow x:Key="mainWindowViewModel"/>
</Window.DataContext>

答案 1 :(得分:0)

有点晚了,但我正在使用一个简单而完美的解决方案,以防人们仍在寻找可能性:

<Window x:Class="Project.MainWindow"
        Title="{Binding DataContext.ApplicationTitle, ElementName=TheMainView}">

        <views:MainView x:Name="TheMainView"/>

</Window>

简单地说,只需在MainViewModel中添加一个Property,它就是MainView的DataContext,如下所示:

public string ApplicationTitle
        {
            get
            {
                var text = "Application Name";
                if (!string.IsNullOrEmpty(_currentFileLoaded))
                {
                    text += $" ({_currentFileLoaded})";
                }

                return text;
            }
        }

希望有所帮助......

相关问题