我正在开发Silverlight 4应用程序并希望实现看似简单的任务: 我有这个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>
现在我正在为TemplateColumn使用DataTemplate,它使用像这样的工具包Contextmenu
<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>
这里有趣的部分:如果我想能够在一行中的任何地方右键单击并激活上下文菜单,我是否真的必须复制粘贴整个上下文菜单datatemplate FOR EACH列?我不能将它设置为整行吗? stackoverflow上的其他帖子似乎没有给出这个特定问题的答案。
答案 0 :(得分:1)
您可以尝试添加列名,然后调用ContextMenuService.SetContextMenu将ContextMenu添加到其他列。
下一个挑战是确定右键单击了哪一列。
相关帖子:
How to get the Owner of the ContextMenu (from Silverlight 4 toolkit)?
Silverlight Toolkit ContextMenu: Which control was right-clicked?
答案 1 :(得分:1)
根据帖子https://stackoverflow.com/a/8795056/451495的建议 您可以使用以下内容:
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);
}
}