WPF Binding CommandParameter to Element of perent window

374 Views Asked by At

I build some WPF application that as MainView, inside this MainView I have a button that opens a setting window and a Model that I want to be able to show or hide from this settings view. (the show/hide button was originally on the MainView itself) the problem is that I do not know how to use the CommandParameter in the SettingsView to bind to an element In the MainViw.

the element in the MainView:

        <Dev:DevUnitView x:Name="DevData" DataContext="{Binding MainModel.DevUnit}" Visibility="Visible" Margin="994,656,74,-83"/>

the button in the SettingsView:

        <Button Content="Show/Hide debug values" Command="{Binding Settings.DevVisibilityCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Dev:DevUnitView}}, Path=DataContext}" Margin="156,22,180,349"/>

I also tried to Bind to the MainView but I do not know how to find the element I need from there:

CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type mainWindow:MainView}}, Path=DevUnit}"

in this case, it cannot resolve the "DevUnit" in the data context, I also tried by name and other variation of this without success.

the old button that worked in the MainView:

   <Button Content="DEV" Command="{Binding Settings.DevVisibilityCommand}" CommandParameter="{Binding ElementName=DevData}" Margin="406,359,810,259"/>

now no matter how I try to bind it I getting Null UserControl.

0

There are 0 best solutions below