Search code examples
wpfxamldata-bindingattached-properties

Binding an AttachedProperty directly on a Window


Question: Using XAML, how can you set an Attached Property directly on a Window when the value is a complex value?

Explanation:

Note that I've removed some of the XAML code here and there for the sake of brevity.

I am trying to set an Attached Property directly on a WPF window. Normally, it can be set as a property of a window like so:

<Window 
xmlns:data="clr-namespace:MVVM_Test.Data"
data:AttachedProperties.RegisterCommandBindings="somevalue" />

This is fine if the property only requires a simple value (or even a simple binding). However, I want to set the attached property to a complex value using a MultiBinding. I have this working when I make the Attached Property a member of my Grid:

<Window>
    <Window.Resources>
        <data:BindingConverter x:Key="RegisterCommandBindingsConverter" />
    </Window.Resources>
    <Grid>
        <data:AttachedProperties.RegisterCommandBindings>
            <MultiBinding Converter="{StaticResource RegisterCommandBindingsConverter}">
                <Binding RelativeSource="{RelativeSource Mode=Self}" Path="(data:AttachedProperties.BaseBindings)" />
                <Binding ElementName="automobileView"  Path="DataContext.CommandBindings" />
            </MultiBinding>
        </data:AttachedProperties.RegisterCommandBindings>

However, I want the attached property to reside on the Window, not on the Grid. While having the Attached Property on the Grid does exactly what I need it to, it bothers me that I can't figure out how to set it on Window.

If I put the Attached Property binding as the first member of Window, before Window.Resources, I get a runtime exception from the Window's XAML stating:

'Provide value on 'System.Windows.StaticResourceExtension' threw an exception.' Line number '6' and line position '9'.

with an inner exception of:

Cannot find resource named 'RegisterCommandBindingsConverter'. Resource names are case sensitive.

If I put the Attached Property binding after Window.Resources but still as a direct member of Window and before the Grid, I get the following error at compile time:

The object 'Window' already has a child and cannot add ''. 'Window' can accept only one child. Line 42 Position 11.


Solution

  • The wrong thing in what you want to do is that you can not use any item (converter in this case) defined in the window resources, in the same window. You must to define the converter in an outter resource dictionary, like the App.xaml's ResourcesDictionary. Take a look to this code:

    <Application x:Class="WpfApplication3.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <data:BindingConverter x:Key="RegisterCommandBindingsConverter" />
    </Application.Resources>
    

    Try this, I think should work. Hope helps...