Context menu in Datatemplate
-
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???
-
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???
mikla521 wrote:
But how do I implement the click event???
Which click event?
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
mikla521 wrote:
But how do I implement the click event???
Which click event?
Mark Salsbery Microsoft MVP - Visual C++ :java:
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
-
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
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:
-
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:
-
I have created a separate file with my templates and style and use <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="resourceDic.xaml"/> </ResourceDictionary.MergedDictionaries>
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:
-
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:
-
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...
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: