Shawn Andrews
Shawn Andrews

Reputation: 1442

XamlParseException when binding to a x:static property

I have the following static property in the MenuVM.cs static class:

public static Thickness getMenuCatalogItemMargin
{
    get { return new Thickness(getMenuItemsWidth * 0.012, getMenuItemsWidth * 0.012, getMenuItemsWidth * 0.012, getMenuItemsWidth * 0.012); }
}

Which im trying to access on line 29 in XAML via:

Margin="{Binding Path={x:Static local:MenuVM.getMenuCatalogItemMargin}, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"

The following error suggests it cannot interpret my notation so is this the correct way to call a static property with a RelativeSource parameter?

Error:

System.Windows.Markup.XamlParseException: ''Set property 'System.Windows.Data.Binding.Path' threw an exception.' Line number '29' and line position '110'.'

InvalidCastException: Unable to cast object of type 'System.Windows.Thickness' to type 'System.Windows.PropertyPath'.

Upvotes: 0

Views: 581

Answers (1)

Clemens
Clemens

Reputation: 128061

Since WPF 4.5 you bind to a static property with this syntax:

Margin="{Binding Path=(local:MenuVM.getMenuCatalogItemMargin)}"

Before 4.5, you would have used

Margin="{Binding Source={x:Static local:MenuVM.getMenuCatalogItemMargin}}"

Upvotes: 1

Related Questions