Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

ItemsPresenter bind CommandParameter to item

Posted on 2010-08-16
2
Medium Priority
?
1,299 Views
Last Modified: 2012-08-13
I have an ItemsPresenter with a context menu.  In the context menu there is one menuitem and I want to bind the CommandParameter of the MenuItem to the Item inside of the ItemsPresenter. But I'm having trouble getting to the Item.  How Do I bind the CommandParameter to the Item?  The item is a ViewModel Object I created.

                                                                <ItemsPresenter Margin="24,0,0,0" >
                                                                    <ItemsPresenter.ContextMenu>
                                                                        <ContextMenu>
                                                                           <MenuItem Header="Remove file from OCR list" Command="{x:Static commands:OCRCommands.RemoveFile}"   CommandParameter="{Binding}">
                                                                                <MenuItem.Icon>
                                                                                    <Image Source="/ThemeManager;component/Images/Delete - Copy.png" Width="15"  Margin="5"/>
                                                                                </MenuItem.Icon>
                                                                            </MenuItem>
                                                                        </ContextMenu>
                                                                    </ItemsPresenter.ContextMenu>
                                                                </ItemsPresenter>
<GroupStyle x:Key="groupItemStyle">
                                <GroupStyle.ContainerStyle>
                                    <Style TargetType="{x:Type GroupItem}">
                                        <Setter Property="Margin" Value="0,0,0,5"/>
                                        <Setter Property="Template">
                                            <Setter.Value>
                                                <ControlTemplate TargetType="{x:Type GroupItem}">
                                                    <Expander IsExpanded="True" Background="AliceBlue">
                                                        <Expander.Header>
                                                            <DockPanel>
                                                                <TextBlock Text="{Binding Path=Name}" Margin="5,0,0,5" Width="200" />
                                                                <StackPanel Orientation="Horizontal">
                                                                    <TextBlock Text="(" />
                                                                    <TextBlock Text="{Binding Path=ItemCount}" />
                                                                    <TextBlock Text=" items)" />
                                                                </StackPanel>
                                                            </DockPanel>
                                                        </Expander.Header>
                                                        <Expander.Content>
                                                            <Border Background="White" Margin="2" CornerRadius="3">
                                                                <ItemsPresenter Margin="24,0,0,0" >
                                                                    <ItemsPresenter.ContextMenu>
                                                                        <ContextMenu>
                                                                            <MenuItem Header="Remove file from OCR list" Command="{x:Static commands:OCRCommands.RemoveFile}"   CommandParameter="{Binding}">
                                                                                <MenuItem.Icon>
                                                                                    <Image Source="/Dms.ThemeManager;component/Images/Delete - Copy.png" Width="15"  Margin="5"/>
                                                                                </MenuItem.Icon>
                                                                            </MenuItem>
                                                                        </ContextMenu>
                                                                    </ItemsPresenter.ContextMenu>
                                                                </ItemsPresenter>
                                                            </Border>
                                                        </Expander.Content>
                                                    </Expander>
                                                </ControlTemplate>
                                            </Setter.Value>
                                        </Setter>
                                    </Style>
                                </GroupStyle.ContainerStyle>
                            </GroupStyle>

Open in new window

0
Comment
Question by:OFGemini
  • 2
2 Comments
 

Author Comment

by:OFGemini
ID: 33464861
Hello,

I've added screenshot of when my Execute Command Handler is called.  When I bind using CommandParameter="{Binding}"  I get an object of the MS.Internal.DataCollectionViewGroupInternal.
In Debug mode I looked inside this object and I see it as a collection and the Item I really wanted to bind to is present in the collection.  My question is how to I bind directly to my object via XAML.

I've attached a screenshot showing what I got and What I'm trying to get.


Capture.PNG
0
 

Accepted Solution

by:
OFGemini earned 0 total points
ID: 33465218
Hi,

I found the solution after looking at this article:

http://blogs.msdn.com/b/vinsibal/archive/2009/01/21/exploring-mvvm-grouping-with-the-datagrid.aspx


The key was to bind to Items[0].

<MenuItem Header="Remove file from OCR list"
                                                                                      Command="{x:Static commands:OCRCommands.RemoveFile}"
                                                                                      CommandParameter="{Binding Path=Items[0]}">



<ItemsPresenter Margin="24,0,0,0" >
                                                                    <ItemsPresenter.ContextMenu>
                                                                        <ContextMenu>
                                                                            <MenuItem Header="Remove file from OCR list" 
                                                                                      Command="{x:Static commands:OCRCommands.RemoveFile}"
                                                                                      CommandParameter="{Binding Path=Items[0]}">
                                                                                <MenuItem.Icon>
                                                                                    <Image Source="/Dms.ThemeManager;component/Images/Delete - Copy.png" Width="15"  Margin="5"/>
                                                                                </MenuItem.Icon>
                                                                            </MenuItem>
                                                                        </ContextMenu>
                                                                    </ItemsPresenter.ContextMenu>
                                                                </ItemsPresenter>

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question