I'm developing a Silverlight 4 application and want to achieve a seemingly easy task: I have this DataGrid:
<sdk:DataGrid x:Name="dg" Grid.Row="1" Grid.ColumnSpan="2" Width="800" ItemsSource="{Binding SearchResults}" AutoGenerateColumns="False" >
<sdk:DataGrid.Columns>
<sdk:DataGridTemplateColumn SortMemberPath="Name" Header="Name" CellTemplate="{StaticResource SearchGridNameFieldtemplate}">
</sdk:DataGridTemplateColumn>
<sdk:DataGridTemplateColumn SortMemberPath="samAccountName" Header="Kontoname" CellTemplate="{StaticResource SearchGridAccountNameFieldtemplate}">
</sdk:DataGridTemplateColumn>
<sdk:DataGridTextColumn Header="Description"
Binding="{Binding Principal.Description}" />
<sdk:DataGridTextColumn Header="Container"
Binding="{Binding Principal.ContainerDN}" />
</sdk:DataGrid.Columns>
</sdk:DataGrid>
Now I am using a DataTemplate for the TemplateColumn which uses a toolkit Contextmenu like this
<DataTemplate x:Key="DirectoryUserSearchItemViewModel" >
<StackPanel Orientation="Horizontal">
<controlsInputToolkit:ContextMenuService.ContextMenu>
<controlsInputToolkit:ContextMenu>
<controlsInputToolkit:MenuItem Header="Eigenschaften" Command="{Binding EditItem}" Visibility="{Binding User.DistinguishedName, Converter={StaticResource UserPermissionVisibilityConverter},ConverterParameter=1;8}" CommandParameter="{Binding User}">
<controlsInputToolkit:MenuItem.Icon>
<Rectangle Width="16" Height="16" Fill="{StaticResource MoveUpBrush}"/>
</controlsInputToolkit:MenuItem.Icon>
</controlsInputToolkit:MenuItem>
<controlsInputToolkit:MenuItem Header="User endgültig löschen" Command="{Binding HardDeleteAccount}" Visibility="{Binding HardDeleteCommandVisibility}" CommandParameter="{Binding User}">
<controlsInputToolkit:MenuItem.Icon>
<Rectangle Width="16" Height="16" Fill="{StaticResource MoveUpBrush}"/>
</controlsInputToolkit:MenuItem.Icon>
</controlsInputToolkit:MenuItem>
<controlsInputToolkit:MenuItem Header="Externe Anwendung ausführen" Command="{Binding ExecuteExternalApplication}" CommandParameter="{Binding User}">
<controlsInputToolkit:MenuItem.Icon>
<Rectangle Width="16" Height="16" Fill="{StaticResource MoveUpBrush}"/>
</controlsInputToolkit:MenuItem.Icon>
</controlsInputToolkit:MenuItem>
</controlsInputToolkit:ContextMenu>
</controlsInputToolkit:ContextMenuService.ContextMenu>
<TextBlock Text="{Binding Principal.Name}"/>
</StackPanel>
</DataTemplate>
Here's the fun part: Do I really have to copy paste the whole context menu datatemplate FOR EACH column in my grid if I want to be able to right click anywhere in a row and activate the context menu? Can't I set this for an entire row? Other posts on stackoverflow didnt seem to give an answer to this specific problem.
As recomended in post https://stackoverflow.com/a/8795056/451495 You can use following:
public partial class Page1 : UserControl
{
public Page1()
{
InitializeComponent();
Loaded += OnLoaded;
Grid1.LoadingRow += Grid1OnLoadingRow;
}
private void Grid1OnLoadingRow(object sender, DataGridRowEventArgs e)
{
var contextMenu = new ContextMenu();
var deleteMenuItem = new System.Windows.Controls.MenuItem { Header = "text" };
contextMenu.Items.Add(deleteMenuItem);
ContextMenuService.SetContextMenu(e.Row, contextMenu);
}
}