0

I have a data-binding that relies on the window in which it is defined.

Trying to create the binding like thus :

<Binding Source="{RelativeSource Self}"/>

results in an error.

I want the binding to resolve to the window itself... how can I accomplish this?

This has been marked as a duplicate of a couple of questions, however the method described therein is the method I am trying here and it is not working.

I am making use of a MultiConverter that I wrote which expects two bindings - one to a boolean, and one to a window :

<MultiBinding Converter="{c:MyMultiConverter}">
    <MultiBinding.ConverterParameter>
        <sys:Int32>0</sys:Int32>
    </MultiBinding.ConverterParameter>
    <!--This binding works fine-->
    <Binding Path="MyBooleanProperty" Source="{x:Static MyPropertySource}"/>

    <!--This binding results in an error - 'Value cannot be null.'-->
    <Binding Source="{RelativeSource Self}"/>
</MultiBinding>

This is the gist of the converters Convert function :

public object Convert(
    object[ ] values, Type targetType,
    object parameter, CultureInfo culture ) {
    int
        //Get the monitor number the window is currently on...
        Monitor = Screen.AllScreens.ToList( ).IndexOf( ( values[1] as Window ).Screen( ) ), 
        //[0] : If true, multiply by 2; else by 1. Add Parameter.
        Index =  ( Monitor * ( ( bool ) values[0] ? 2 : 1 ) ) + ( int )parameter;
    return MyProject.MyList[Index];
}

Window.Screen( ) is just a simple extension method which returns the screen the window is located on.

Debugging reveals that attempting to resolve values[1] as Window results in null...

Will
  • 3,413
  • 7
  • 50
  • 107
  • 1
    Possible duplicate of [How do I use WPF bindings with RelativeSource?](http://stackoverflow.com/questions/84278/how-do-i-use-wpf-bindings-with-relativesource) – ASh Jun 10 '16 at 09:30
  • Possible duplicate of [WPF Bind to itself](http://stackoverflow.com/questions/1906587/wpf-bind-to-itself) – Fruchtzwerg Jun 10 '16 at 09:31

1 Answers1

1
{Binding RelativeSource={RelativeSource AncestorType=Window}}
Funk
  • 10,976
  • 1
  • 17
  • 33