Search code examples
c#.netwpfxamldata-binding

BindingExpression path error: 'SystemParameters'


While updating my software, I started facing this BindingExpression error.

System.Windows.Data Error: 40 : BindingExpression path error: 'SystemParameters' property not found on 'object' ''MainViewModel' (HashCode=4781813)'. BindingExpression:Path=SystemParameters.PrimaryScreenHeight; DataItem='MainViewModel' (HashCode=4781813); target element is 'MainWindow' (Name='XXX'); target property is 'Height' (type 'Double')

System.Windows.Data Error: 40 : BindingExpression path error: 'SystemParameters' property not found on 'object' ''MainViewModel' (HashCode=4781813)'. BindingExpression:Path=SystemParameters.PrimaryScreenWidth; DataItem='MainViewModel' (HashCode=4781813); target element is 'MainWindow' (Name='XXX'); target property is 'Width' (type 'Double')*

Reading the above error, it seems that it couldn't find the SystemParameters object in MainViewModel.

<Window x:Name="XXXX" x:Class="XXXX.Views.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:XXXX"
    xmlns:localize="http://gu.se/Localization"
    xmlns:properties="clr-namespace:XXXX.Properties"
    mc:Ignorable="d"
    d:DesignHeight="1080" d:DesignWidth="1920"
    Title=""
    WindowStyle="None"
    ResizeMode="NoResize"
    WindowStartupLocation="CenterScreen" WindowState="{Binding WindowState, Mode=TwoWay}"
    DataContext="{Binding Main, Source ={StaticResource Locator}}"
    Height="{Binding SystemParameters.PrimaryScreenHeight}" 
    Width="{Binding SystemParameters.PrimaryScreenWidth}" Background="#FFCCCCCC">

   <Window.Resources>

When I start my application, the Initializer.cs class is called which initializes all the parameters and creates an instance of MainWindow. By debugging, I discovered that the issue seems to appear when I call the method window.Show() and then my application crashes instantly.

MainWindow window = new MainWindow();
window.Show();

Solution

  • A Binding will resolve the property path using the DataContext of an element by default (which is inherited). The PrimaryScreenHeight and PrimaryScreenWidth properties are static and part of the type SystemParameters, which is a static type, that is not part of the data context. To solve the issue you can:

    • Use the x:Static markup extension to refer to static properties.

      Implements a markup extension that returns static field and property references.

      Height="{x:Static SystemParameters.PrimaryScreenHeight}"
      Width="{x:Static SystemParameters.PrimaryScreenWidth}"
      
    • If you need to apply a value converter, you can use the x:Static markup extension for the Source.

      Height="{Binding Source={x:Static SystemParameters.PrimaryScreenHeight}, Converter={StaticResource MyConverter}}"
      Width="{Binding Source={x:Static SystemParameters.PrimaryScreenWidth}, Converter={StaticResource MyConverter}}"