Solved

ItemsPresenter bind CommandParameter to item

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Where did System.Data.Objects go? 2 41
What Does This C# Code Block Do? 5 58
MailAddress in vb 4 29
VS 2013 installer,  Install, Update and uninstall via SCCM 6 26
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…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

828 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