how can I activate a menu from a button inside of an itemtemplate?

Jan 20, 2011 at 5:03 PM

First of all, nice work!

How, without resorting to code (only using XAML) can I get a menu to pop-up when a user clicks on a button within an itemtemplate? I tried the following, but the menu only pops up when I click on the button in the first item in the listBox:

		<DataTemplate x:Key="DataTemplate1">
			<Grid>
				<Grid.RowDefinitions>
					<RowDefinition/>
					<RowDefinition Height="11"/>
				</Grid.RowDefinitions>
				<SL4PopupMenu:PopupMenu d:LayoutOverrides="Height" Grid.Row="1" LeftClickElements="MenuActivator">
					<ListBox>
						<SL4PopupMenu:PopupMenuItem Header="One"/>
						<SL4PopupMenu:PopupMenuItem Header="Two"/>
						<SL4PopupMenu:PopupMenuItem Header="Three"/>
						<SL4PopupMenu:PopupMenuItem Header="Four"/>
					</ListBox>
				</SL4PopupMenu:PopupMenu>
				<StackPanel Orientation="Horizontal" d:LayoutOverrides="Width, Height">
					<Button x:Name="MenuActivator" Content="!" Width="22"/>
					<TextBlock Text="{Binding Property1}" Margin="4,0,0,0" VerticalAlignment="Center"/>
				</StackPanel>
			</Grid>
		</DataTemplate>
and this is used in a list box:
<ListBox x:Name="DataDrivenListBox2" Grid.Column="2" ItemTemplate="{StaticResource DataTemplate1}" ItemsSource="{Binding Collection}" Grid.Row="1"/>
thanks,
- Dan
Feb 8, 2011 at 4:27 PM

I had experienced similar issue - having PopupMenu tied inside template cause unpredictable behavior. I think it is a bug and have fixed it using a little patch of PopupMenuUtils.cs:

 

        public static FrameworkElement FindApplicationElementByName(FrameworkElement self, string elementName, string elementQualifierForErrorMsg)
        {
            object obj = ((FrameworkElement) self.Parent).FindName(elementName.Trim());
            if (obj == null) // Object not found in neighbourhood. Search down from root
                obj = (Application.Current.RootVisual as FrameworkElement).FindName(elementName.Trim());

                <...>

 

and corrected a usage of this method in couple of places due to introduced parameter self

Feb 8, 2011 at 4:44 PM
BenKalegin wrote:

I had experienced similar issue - having PopupMenu tied inside template cause unpredictable behavior. I think it is a bug and have fixed it using a little patch of PopupMenuUtils.cs:

 

        public static FrameworkElement FindApplicationElementByName(FrameworkElement self, string elementName, string elementQualifierForErrorMsg)
        {
            object obj = ((FrameworkElement) self.Parent).FindName(elementName.Trim());
            if (obj == null) // Object not found in neighbourhood. Search down from root
                obj = (Application.Current.RootVisual as FrameworkElement).FindName(elementName.Trim());

                <...>

 

and corrected a usage of this method in couple of places due to introduced parameter self

 So, pardon my ignorance, but where/how do I get/use this patch?

thanks,

- Dan

Coordinator
Mar 7, 2011 at 2:19 AM

Thanks for sending the required modifications BenKalegin. I included them in the latest release downloadable from here:

http://sl4popupmenu.codeplex.com/SourceControl/list/changesets