Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Context menu in Datatemplate

Context menu in Datatemplate

Scheduled Pinned Locked Moved WPF
questioncsswpfwcfhelp
8 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mikla521
    wrote on last edited by
    #1

    Next problem.... Here's some code <DataTemplate x:Key="TestDataListTemplate"> <Border Margin="5" BorderThickness="1" BorderBrush="Black" CornerRadius="4"> <Grid Margin="5,2"> <Grid.RowDefinitions> <RowDefinition></RowDefinition> <RowDefinition></RowDefinition> </Grid.RowDefinitions> <TextBlock Grid.Row="0" Text="{Binding Path=Usage}" Foreground="Black" FontWeight="Bold" FontFamily="Courier" FontSize="12"></TextBlock> <TextBlock Grid.Row="1" FontFamily="Arial" Foreground="DarkBlue" Text="{Binding Path=Path, UpdateSourceTrigger=PropertyChanged}" FontSize="12"></TextBlock> </Grid> </Border> </DataTemplate> ...which I use as a ListBoxItem. If I want a context menu for each item like this, I manage to get the menu, editing the code to... ...<Grid Margin="5,2"> <Grid.ContextMenu> <ContextMenu> <MenuItem Header="Delete" /> </ContextMenu> </Grid.ContextMenu>... But how do I implement the click event???

    M 1 Reply Last reply
    0
    • M mikla521

      Next problem.... Here's some code <DataTemplate x:Key="TestDataListTemplate"> <Border Margin="5" BorderThickness="1" BorderBrush="Black" CornerRadius="4"> <Grid Margin="5,2"> <Grid.RowDefinitions> <RowDefinition></RowDefinition> <RowDefinition></RowDefinition> </Grid.RowDefinitions> <TextBlock Grid.Row="0" Text="{Binding Path=Usage}" Foreground="Black" FontWeight="Bold" FontFamily="Courier" FontSize="12"></TextBlock> <TextBlock Grid.Row="1" FontFamily="Arial" Foreground="DarkBlue" Text="{Binding Path=Path, UpdateSourceTrigger=PropertyChanged}" FontSize="12"></TextBlock> </Grid> </Border> </DataTemplate> ...which I use as a ListBoxItem. If I want a context menu for each item like this, I manage to get the menu, editing the code to... ...<Grid Margin="5,2"> <Grid.ContextMenu> <ContextMenu> <MenuItem Header="Delete" /> </ContextMenu> </Grid.ContextMenu>... But how do I implement the click event???

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      mikla521 wrote:

      But how do I implement the click event???

      Which click event?

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      M 1 Reply Last reply
      0
      • M Mark Salsbery

        mikla521 wrote:

        But how do I implement the click event???

        Which click event?

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        M Offline
        M Offline
        mikla521
        wrote on last edited by
        #3

        Sorry....like this then... <DataTemplate x:Key="TestDataListTemplate" > ... <Grid Margin="5,2"> <Grid.ContextMenu> <ContextMenu> <MenuItem Header="Delete" Click="MenuItem_Click_DeleteProduct" /> </ContextMenu> </Grid.ContextMenu>... Error:ResourceDictionary root element requires a x:Class attribute... If I add x:class="MyProgram.Window1" to <ResourceDictionary the i get this instead, among a few others... Error: Partial declarations of MyProgram.Window1 must not specify different base classes....don't get it. :sigh: Well, it seems like I have more problems. The goal is to have ListBox with DataTemplate items, then I want a ContextMenu where I can choose to delete an item... Maybe I have to think in another way.

        modified on Sunday, April 12, 2009 2:41 AM

        M 1 Reply Last reply
        0
        • M mikla521

          Sorry....like this then... <DataTemplate x:Key="TestDataListTemplate" > ... <Grid Margin="5,2"> <Grid.ContextMenu> <ContextMenu> <MenuItem Header="Delete" Click="MenuItem_Click_DeleteProduct" /> </ContextMenu> </Grid.ContextMenu>... Error:ResourceDictionary root element requires a x:Class attribute... If I add x:class="MyProgram.Window1" to <ResourceDictionary the i get this instead, among a few others... Error: Partial declarations of MyProgram.Window1 must not specify different base classes....don't get it. :sigh: Well, it seems like I have more problems. The goal is to have ListBox with DataTemplate items, then I want a ContextMenu where I can choose to delete an item... Maybe I have to think in another way.

          modified on Sunday, April 12, 2009 2:41 AM

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          Where is this DataTemplate resource located? It looks like there's no problem with the template but there's a problem with where the template an/or the click handler code is located... Mark

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          M 1 Reply Last reply
          0
          • M Mark Salsbery

            Where is this DataTemplate resource located? It looks like there's no problem with the template but there's a problem with where the template an/or the click handler code is located... Mark

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            M Offline
            M Offline
            mikla521
            wrote on last edited by
            #5

            I have created a separate file with my templates and style and use <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="resourceDic.xaml"/> </ResourceDictionary.MergedDictionaries>

            M 1 Reply Last reply
            0
            • M mikla521

              I have created a separate file with my templates and style and use <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="resourceDic.xaml"/> </ResourceDictionary.MergedDictionaries>

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #6

              You'll need an x:class set on your resource dictionary that references the class containing the click handler. For example:

              <!-- Dictionary1.xaml -->

              <ResourceDictionary
              x:Class="WPFTester.DictionaryHandlerClass"
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              xmlns:local="clr-namespace:WPFTester">

              <DataTemplate x:Key="TestDataListTemplate" DataType="{x:Type local:Item}">
                  <Border BorderBrush="SteelBlue" BorderThickness="4" >
                      <Grid Background="LightSteelBlue" Height="20" >
                          <Grid.ContextMenu>
                              <ContextMenu>
                                  <MenuItem Header="Delete" Click="MenuItem\_Click" />
                              </ContextMenu>
                          </Grid.ContextMenu>
                      </Grid>
                  </Border>
              </DataTemplate>
              

              </ResourceDictionary>

              // DictionaryHandlerClass.cs

              using System;
              using System.Windows;

              namespace WPFTester
              {
              public partial class DictionaryHandlerClass
              {
              private void MenuItem_Click(object sender, RoutedEventArgs e)
              {
              }
              }
              }

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              M 1 Reply Last reply
              0
              • M Mark Salsbery

                You'll need an x:class set on your resource dictionary that references the class containing the click handler. For example:

                <!-- Dictionary1.xaml -->

                <ResourceDictionary
                x:Class="WPFTester.DictionaryHandlerClass"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:WPFTester">

                <DataTemplate x:Key="TestDataListTemplate" DataType="{x:Type local:Item}">
                    <Border BorderBrush="SteelBlue" BorderThickness="4" >
                        <Grid Background="LightSteelBlue" Height="20" >
                            <Grid.ContextMenu>
                                <ContextMenu>
                                    <MenuItem Header="Delete" Click="MenuItem\_Click" />
                                </ContextMenu>
                            </Grid.ContextMenu>
                        </Grid>
                    </Border>
                </DataTemplate>
                

                </ResourceDictionary>

                // DictionaryHandlerClass.cs

                using System;
                using System.Windows;

                namespace WPFTester
                {
                public partial class DictionaryHandlerClass
                {
                private void MenuItem_Click(object sender, RoutedEventArgs e)
                {
                }
                }
                }

                Mark Salsbery Microsoft MVP - Visual C++ :java:

                M Offline
                M Offline
                mikla521
                wrote on last edited by
                #7

                Ok, thanks :) I tried that before but it doesn't seems like it's possible to use the startup Window1 class. Think I have to create a separate class behind my resource file and go from there...

                M 1 Reply Last reply
                0
                • M mikla521

                  Ok, thanks :) I tried that before but it doesn't seems like it's possible to use the startup Window1 class. Think I have to create a separate class behind my resource file and go from there...

                  M Offline
                  M Offline
                  Mark Salsbery
                  wrote on last edited by
                  #8

                  mikla521 wrote:

                  it doesn't seems like it's possible to use the startup Window1 class

                  Yes. ResourceDictionary is a different class than Window :) Mark

                  Mark Salsbery Microsoft MVP - Visual C++ :java:

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups