Solved

ItemsPresenter bind CommandParameter to item

Posted on 2010-08-16
2
1,253 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Suggested Solutions

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…

752 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