Unable to Bind Window Title to MVVM Data Context

0

I have a Net Framework 4.7.2. Application that I'm writing in WPF/C#.

I'm trying to bind the Main Windows' Title to a property on its Data Context, PartyName, but I keep getting an error at runtime.

Here's the XAML for the window:

    <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="..\Dictionaries\ConverterDictionary.xaml"/>
                    <ResourceDictionary Source="..\Dictionaries\StyleDictionary.xaml"/>
                    <ResourceDictionary Source="..\Dictionaries\TemplateDictionary.xaml"/>
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Window.Resources>
<Grid>
...
</Window>

And this is the Class for the 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>();
        }

    }
}

Whenever I run the app I get an error:

System.Windows.Markup.XamlParseException HResult=0x80131501
Message='Provide value on 'System.Windows.StaticResourceExtension' threw an exception.' Line number '8' and line position '9'.
Source=PresentationFramework StackTrace: at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)

This exception was originally thrown at this call stack: System.Windows.StaticResourceExtension.ProvideValueInternal(System.IServiceProvider, bool) System.Windows.StaticResourceExtension.ProvideValue(System.IServiceProvider) MS.Internal.Xaml.Runtime.ClrObjectRuntime.CallProvideValue(System.Windows.Markup.MarkupExtension, System.IServiceProvider)

Inner Exception 1: Exception: Cannot find resource named 'MainTitle'. Resource names are case sensitive.

Line Number 8, position 9 refers to the 'Title' line in the Windows XAML. Additionally for some reason the element:

<vm:MainWindowVM/>

has an error "Object reference not set to Instance of Object", which i think is related to the failure to Bind.

All the other bindings work fine.

Can any of you clever people advise me as to why I'm unable to bind the widow title, and why I get the "Object reference not set..." error?

Many thanks in advance, and Keep safe and well.

c#
wpf
data-binding

1 Answer

0

Whenever I run the app I get an error:

System.Windows.Markup.XamlParseException HResult=0x80131501 Message='Provide value on 'System.Windows.StaticResourceExtension' threw an exception.' Line number '8' and line position '9'. Source=PresentationFramework StackTrace: at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri) This exception was originally thrown at this call stack: System.Windows.StaticResourceExtension.ProvideValueInternal(System.IServiceProvider, bool) System.Windows.StaticResourceExtension.ProvideValue(System.IServiceProvider) MS.Internal.Xaml.Runtime.ClrObjectRuntime.CallProvideValue(System.Windows.Markup.MarkupExtension, System.IServiceProvider) Inner Exception 1: Exception: Cannot find resource named 'MainTitle'. Resource names are case sensitive.

The reason for this is because of the actual binding on the property. You have a converter on that binding that doesn't exist.

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

You don't have one at all and it's not needed as you just have a string; that doesn't need converting. To fix the error simple remove that converter binding.

 Title="{Binding Path=PartyName}"

Note: As mentioned in the comments by @Clemens, Also remove Mode=OneWay. It's pointless; didn't mention this as it's not a factor to the issue you are seeing.

answered on Stack Overflow Mar 27, 2020 by zaggler • edited Mar 27, 2020 by zaggler

User contributions licensed under CC BY-SA 3.0