简体   繁体   中英

Usercontrol command parameter from parent

In my wpf project I have a usercontrol containing the menu bar for my program. Im not sure thats the right way to do it, but nice way to keep i seperated. This menu bar contains of lot of buttons with command bindings using mvvmlight toolkit eventtocommand, this works perfectly. But I have a print button where I would need to pass a Visual object from mainwindow to the commandparameter in the usercontrol.

Usercontrol.xaml - where MasterGrid is the elementname in mainwindow.

<MenuItem Header="_Print" Command="{Binding PrintDiagramCommand}"
          CommandParameter="{Binding ElementName=MasterGrid}" 
          InputGestureText="Ctrl+P">
   <MenuItem.Icon>
       <Image Height="24" Width="24" Source="/Resources/Icons/print.ico"/>
   </MenuItem.Icon>
</MenuItem>

I would really appreciate your help.

Try this , though I have'nt tested it . there is a difference in ElementName and x:Reference of how the element is been found as contextmenu is not the part of VisualTree so ElementName wont work.

CommandParameter="{Binding Source={x:Reference MasterGrid}}"

I hope this will help.

Note: Its not a good practice to access elements in ViewModel.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM