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

Tim*_*Tim 9 c# data-binding wpf 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>
Run Code Online (Sandbox Code Playgroud)

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

以下是View Model的代码:

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
Run Code Online (Sandbox Code Playgroud)

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

是在Code Behind中设置DataContext的唯一解决方案吗?我可以让它工作,但我宁愿把它保存在XAML中.

jur*_*ure 12

您可以尝试设置DataContextusing属性元素语法:

<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>
Run Code Online (Sandbox Code Playgroud)

这应该起作用,因为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>
Run Code Online (Sandbox Code Playgroud)