views:

37

answers:

1

I have a ListView that allows the user to change the ViewBase through a Context Menu (it acts like a simplified version of windows explorer).

 <ListView Name="lv" Grid.Row ="0" Grid.Column ="1"  >
        <ListView.ContextMenu>
            <ContextMenu>
                <MenuItem Header="View1" Click="SwitchViewMenu"/>
                <MenuItem Header="View2" Click="SwitchViewMenu"/>                   
            </ContextMenu>
        </ListView.ContextMenu>
        <ListView.View>
            <local:View1 />
        </ListView.View>
    </ListView>

The ViewBases DataTemplates are defined in a Generic.XAML file, and i use the following Function to change the chosen view :

 void ChangeView(string str)
    {
        if (str == "View1")
        {
            lv.View = lv.FindResource("View1") as ViewBase;
        }
        else if (str == "View2")
        {
            lv.View = lv.FindResource("View2") as ViewBase;
        }
    }

The problem:

I got a custom CheckBox control in all the DataTemplates, that has a predefined click event attached, however when i try to move up the Parents of the CheckBox, the highest level i can reach is the Parenting Grid in the DataTemplate in use.

What i need to access is the Parent Window itself.

Note: I tried to add a Dependency Property to the Custom CheckBox Control and bind it to an extra defined variable in the sent object (The data template's items DataType object) that had a window reference as its value, but i kept getting null even though all the other dependency properties/values got bound.

+1  A: 

It depends on how you're trying to get to the parents. I highly recommend this article (Josh Smith) to get a real understanding of what's going on in WPF.

That said, you could try Window.GetWindow(myControl); (static method); it should work for any pure WPF trees (for interop with WinForms, see this).

Alex Paven
LOL! i actually found a workaround eventually that did the trick.. however your solution worked perfectly,in an easier way and with less Code. (+1) .
Madi D.