Silverlight DataGrid:整个行上的工具包上下文菜单

发布于 2024-12-09 05:59:40 字数 4003 浏览 0 评论 0原文

我正在开发一个 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,它使用这样的工具包上下文菜单

<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>

这是有趣的部分:如果我希望能够,我是否真的必须复制粘贴网格中每个列的整个上下文菜单数据模板右键单击连续任意位置并激活上下文菜单?我不能为整行设置这个吗? stackoverflow 上的其他帖子似乎没有给出这个具体问题的答案。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一个人练习一个人 2024-12-16 05:59:40

正如帖子 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);
    }
}

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);
    }
}
想念有你 2024-12-16 05:59:40

您可以尝试添加列名称,然后调用 ContextMenuService.SetContextMenu 将 ContextMenu 添加到其他列。

下一个挑战是确定右键单击了哪一列。

相关帖子:
如何获取 ContextMenu 的所有者(来自 Silverlight 4 工具包)?
Silverlight Toolkit ContextMenu:右键单击了哪个控件?< br>

You might try adding column names and then calling ContextMenuService.SetContextMenu to add the ContextMenu to the other columns.

The next challenge is determining which column was right-clicked.

Related posts:
How to get the Owner of the ContextMenu (from Silverlight 4 toolkit)?
Silverlight Toolkit ContextMenu: Which control was right-clicked?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文