Solved

ItemsPresenter bind CommandParameter to item

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Chat Room 1 43
MVC5 Dataformat String for Phone not working 3 25
Linq Help 1 35
In WPF / C# binding a local database in code behind 1 24
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
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…

911 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

16 Experts available now in Live!

Get 1:1 Help Now