Will
Will

Reputation: 3585

How can I bind to the window my binding is in?

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...

Upvotes: 0

Views: 67

Answers (1)

Funk
Funk

Reputation: 11221

{Binding RelativeSource={RelativeSource AncestorType=Window}}

Upvotes: 1

Related Questions