Solved

ItemsPresenter bind CommandParameter to item

Posted on 2010-08-16
2
1,212 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now