Silverlight Datagrid 右键单击​​选择

发布于 2024-09-05 08:20:34 字数 127 浏览 0 评论 0原文

有没有办法让右键单击事件在工具包数据网格中选择一行?

我正在使用工具包上下文菜单,它工作得很好,但问题是,只有左键单击才能选择行,如果我希望上下文菜单正常工作,我需要右键单击才能执行此操作。

任何帮助表示赞赏

Is there a way for a right click event to select a row in toolkit datagrid?

I'm using toolkit context menu which works nicely, but the problem is, only left click is able to select rows, and I need right click to be able to do that if I want my context menu to work properly.

Any help is appreciated

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

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

发布评论

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

评论(5

红墙和绿瓦 2024-09-12 08:20:34

您可以在此处找到解决方案。

基本上是这样的:

private void dg_LoadingRow(object sender, DataGridRowEventArgs e)
{
    e.Row.MouseRightButtonDown += new MouseButtonEventHandler(Row_MouseRightButtonDown);
}
void Row_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
    dg.SelectedItem = ((sender) as DataGridRow).DataContext;
}

You can find a solution here.

Basically it goes like this:

private void dg_LoadingRow(object sender, DataGridRowEventArgs e)
{
    e.Row.MouseRightButtonDown += new MouseButtonEventHandler(Row_MouseRightButtonDown);
}
void Row_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
    dg.SelectedItem = ((sender) as DataGridRow).DataContext;
}
请帮我爱他 2024-09-12 08:20:34

他的行为可以为你带来帮助(受到这个 博客文章):

public class SelectRowOnRightClickBehavior : Behavior<DataGrid>
{
    protected override void OnAttached()
    {
        base.OnAttached();

        AssociatedObject.MouseRightButtonDown += HandleRightButtonClick;
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();

        AssociatedObject.MouseRightButtonDown += HandleRightButtonClick;
    }

    private void HandleRightButtonClick(object sender, MouseButtonEventArgs e)
    {
        var elementsUnderMouse = VisualTreeHelper.FindElementsInHostCoordinates(e.GetPosition(null), AssociatedObject);

        var row = elementsUnderMouse
            .OfType<DataGridRow>()
            .FirstOrDefault();

        if (row != null)
            AssociatedObject.SelectedItem = row.DataContext;
    }
}

像这样使用它:

<sdk:DataGrid x:Name="DataGrid" Grid.Row="4" 
                  IsReadOnly="True" 
                  ItemsSource="{Binding MyItems}">
        <i:Interaction.Behaviors>
            <b:SelectRowOnRightClickBehavior/>
        </i:Interaction.Behaviors>
</sdk:DataGrid>

He's a Behavior which will do the trick for you (inspired by this blog post):

public class SelectRowOnRightClickBehavior : Behavior<DataGrid>
{
    protected override void OnAttached()
    {
        base.OnAttached();

        AssociatedObject.MouseRightButtonDown += HandleRightButtonClick;
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();

        AssociatedObject.MouseRightButtonDown += HandleRightButtonClick;
    }

    private void HandleRightButtonClick(object sender, MouseButtonEventArgs e)
    {
        var elementsUnderMouse = VisualTreeHelper.FindElementsInHostCoordinates(e.GetPosition(null), AssociatedObject);

        var row = elementsUnderMouse
            .OfType<DataGridRow>()
            .FirstOrDefault();

        if (row != null)
            AssociatedObject.SelectedItem = row.DataContext;
    }
}

Use it like this:

<sdk:DataGrid x:Name="DataGrid" Grid.Row="4" 
                  IsReadOnly="True" 
                  ItemsSource="{Binding MyItems}">
        <i:Interaction.Behaviors>
            <b:SelectRowOnRightClickBehavior/>
        </i:Interaction.Behaviors>
</sdk:DataGrid>
送舟行 2024-09-12 08:20:34

谢谢好主意。但如果指定了 UnloadingRow 事件可能会更有效。

private void dg_UnloadingRow(object sender, System.Windows.Controls.DataGridRowEventArgs e)
{
    e.Row.MouseRightButtonDown -= Row_MouseRightButtonDown;
}

Thanks good idea. But the with UnloadingRow event could have been more effective had been specified.

private void dg_UnloadingRow(object sender, System.Windows.Controls.DataGridRowEventArgs e)
{
    e.Row.MouseRightButtonDown -= Row_MouseRightButtonDown;
}
听风吹 2024-09-12 08:20:34

Codeplex 上的这个开源项目开箱即用地支持这种行为,并且做的远不止于此:

http://sl4popupmenu.codeplex .com/

This open source project on Codeplex supports this behavior out of the box and does much more than this:

http://sl4popupmenu.codeplex.com/

勿忘初心 2024-09-12 08:20:34

我尝试了一种稍微不同的方法,使用 DataGrid 中的 LoadingRow 事件。如果没有必要,我不喜欢使用该特定事件,但由于我没有处理大量数据,因此效果很好。此示例中唯一没有的是用于执行操作的命令。您可以在 DataContext 对象上使用命令或某种其他机制。

    private void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
    {
        var contextMenu = new ContextMenu();

        var deleteMenuItem = new MenuItem {Header = "Delete User"};

        contextMenu.Items.Add(deleteMenuItem);

        ContextMenuService.SetContextMenu(e.Row, contextMenu);

    }

I tried a slightly different approach using the LoadingRow event in the DataGrid. I don't like using that particular event if I don't have to, but since I wasn't working with large amounts of data, it works out pretty well. The only thing I don't have in this sample is the command to use to perform the action. You could use a command on the DataContext object or some other mechanism.

    private void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
    {
        var contextMenu = new ContextMenu();

        var deleteMenuItem = new MenuItem {Header = "Delete User"};

        contextMenu.Items.Add(deleteMenuItem);

        ContextMenuService.SetContextMenu(e.Row, contextMenu);

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