无法将窗口标题绑定到MVVM数据上下文



我有一个网络框架4.7.2。我正在用WPF/C#编写的应用程序。

我正试图将主窗口的标题绑定到其数据上下文PartyName上的一个属性,但在运行时我一直遇到错误。

这是窗口的XAML:

<Window x:Class="MyDM.View.Windows.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:MyDM.ViewModel"
xmlns:ctrl="clr-namespace:MyDM.View.Controls"
Height="778" 
Width="459"
Title="{Binding Path=PartyName, Mode=OneWay, Converter={StaticResource MainTitle}}"
Background="White" 
WindowStartupLocation="CenterScreen">
<Window.DataContext>
<vm:MainWindowVM/>
</Window.DataContext>
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="..DictionariesConverterDictionary.xaml"/>
<ResourceDictionary Source="..DictionariesStyleDictionary.xaml"/>
<ResourceDictionary Source="..DictionariesTemplateDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
...
</Window>

这是DataContext的类:

public class MainWindowVM : Notifier
{
#region Private Data
private string thePartyName;
private ObservableCollection<PlayerCharacter> theCharacters;
private int theSelectedIndex;
#endregion Private Data
#region Public Properties
public string PartyName
{
get { return thePartyName; }
protected set
{
thePartyName = value;
OnPropertyChanged("PartyName");
}
}
public ObservableCollection<PlayerCharacter> Characters
{ 
get { return theCharacters; }
set
{
theCharacters = value;
OnPropertyChanged("Characters");
}
}
public int SelectedIndex
{
get { return theSelectedIndex; }
set
{
theSelectedIndex = value;
OnPropertyChanged("SelectedIndex");
}
}
public MainWindowVM()
{
thePartyName = string.Empty;
theCharacters = new ObservableCollection<PlayerCharacter>();
}
}
}

每当我运行应用程序时,我都会收到一个错误:

System.Windows.Markup.XamlParseException HResult=0x080131501
消息="在"System.Windows.StaticResourceExtension"上提供值"引发了异常行号"8"和行位置"9">
Source=PresentationFrameworkStackTrace:位于System.Windows.Markup.WpfXamlLoader.Load(XamlReader XamlReader,IXamlObjectWriterFactory writerFactory,布尔skipJournaledProperties,对象rootObject,XamlObjectWriterSettings设置,基于Uri的Uri(

此异常最初是在此调用堆栈中引发的:System.Windows.StaticResourceExtension.ProvideValueInternal(System.IServiceProvider,布尔(System.Windows.StaticResourceExtension.ProvideValue(System.IServiceProvider(MS.Internal.Xaml.Runtime.ClrObjectRuntime.CallProviderValue(System.Windows.Markup.MarkupExtension,System.IServiceProvider(

内部异常1:异常:找不到名为"MainTitle"的资源。资源名称区分大小写。

第8行,位置9指的是Windows XAML中的"标题"行。此外,由于某些原因,元素:

<vm:MainWindowVM/>

有一个错误"Object reference not set to Instance of Object",我认为这与绑定失败有关。

所有其他绑定都可以正常工作。

你们这些聪明的人能告诉我为什么我不能绑定寡妇头衔,为什么我会出现"对象引用未设置…"错误吗?

非常感谢,并保持安全和健康。

每当我运行应用程序时,我都会收到一个错误:

System.Windows.Markup.XamlParseException HResult=0x80131501消息="在"System.Windows.StaticResourceExtension"上提供值引发异常。"行号"8"和行位置"9"。Source=PresentationFrameworkStackTrace:位于System.Windows.Markup.WpfXamlLoader.Load(XamlReader XamlReader、IXamlObjectWriterFactory writerFactory、Boolean skipJournaledProperties、Object rootObject、XamlObjectWriterSettings设置、Uri-baseUri(此异常最初是在此调用堆栈引发的:System.Windows.StaticResourceExtension.ProvideValueInternal(System.IServiceProvider,bool(System.Windows.SStaticResourceExtension.FrovideValue(System.IServicesProvider(MS.Internal.Xaml.Runtime.ClrObjectRuntime.CallProviderValue(System.Windows.Markup.MarkupExtension,System.IServiceProvider(内部异常1:异常:找不到名为"MainTitle"的资源。资源名称区分大小写。

原因是属性上的实际绑定。您在该绑定上有一个不存在的转换器。

Title="{Binding Path=PartyName, Mode=OneWay, Converter={StaticResource MainTitle}}"

你根本没有,也不需要它,因为你只有一个string;这不需要转换。要修复错误,只需删除转换器绑定即可。

Title="{Binding Path=PartyName}"

注:如@Clemens评论中所述,Also remove Mode=OneWay. It's pointless;没有提到这一点,因为这不是你所看到的问题的一个因素。

最新更新