• silverlight datagrid 右键菜单处理


    Toolkit Samples中的示例是将ContextMenu添加到ListBox的ItemTemplate中

    而DataGrid由于没有ItemTemplate,所以稍有不同

    <UserControl 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x
    ="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d
    ="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc
    ="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable
    ="d"
    d:DesignHeight
    ="300" d:DesignWidth="400"
    x:Class
    ="SilverlightApplication8.MainPage"
    xmlns:data
    ="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data">

    <Grid x:Name="LayoutRoot" Background="White">
    <data:DataGrid Name="dg" ItemsSource="{Binding}" LoadingRow="dg_LoadingRow"/>
    </Grid>
    </UserControl>



    添加引用System.Windows.Controls和System.Windows.Controls.Input.Toolkit

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;

    using System.Windows.Data;
    namespace SilverlightApplication8
    {
    publicpartialclass MainPage : UserControl
    {
    PagedCollectionView pcv;

    public MainPage()
    {
    InitializeComponent();

    List<Person> list =new List<Person>();
    list.Add(new Person { ID =1, Name ="张三" });
    list.Add(new Person { ID =2, Name ="李四" });
    list.Add(new Person { ID =3, Name ="王五" });

    pcv =new PagedCollectionView(list);
    dg.ItemsSource = pcv;
    }

    privatevoid MenuItem_Click(object sender, RoutedEventArgs e)
    {
    Person p = ((MenuItem)sender).DataContext as Person;
    if (p !=null)
    {
    pcv.Remove(p);
    }
    }

    privatevoid dg_LoadingRow(object sender, DataGridRowEventArgs e)
    {
    DataGridRow dgr = e.Row;
    Person p = (Person)dgr.DataContext;

    ContextMenu cm =new ContextMenu();
    MenuItem mi =new MenuItem();
    mi.Header ="删除 "+ p.Name;
    mi.Click += MenuItem_Click;
    cm.Items.Add(mi);

    ContextMenuService.SetContextMenu(dgr, cm);
    }
    }

    publicclass Person
    {
    publicint ID { get; set; }
    publicstring Name { get; set; }
    }
    }
    
    
    
    



    这样在点击时需要根据DataGrid的SelectedItem获取选中行

    就会造成当前选中第一行,鼠标放在第三行上右击"删除",最后却会删除第一行的情况

    转自:http://www.cnblogs.com/zhlei616/archive/2010/05/14/1735625.html
  • 相关阅读:
    Android 跳转到系统应用管理
    Android权限管理PermissionsDispatcher2.3.2使用+原生6.0权限使用
    Android权限管理PermissionsDispatcher2.3.2使用+原生6.0权限使用
    Android 6.0 Permission权限与安全机制
    Windows下Mongodb安装及配置
    mongo 3.0 备份和还原数据库 ,及too many positional arguments错误
    maven 将jar 下载到工程当前目录下
    MyBatis两张表字段名相同产生的问题
    svn报错can only be performed on a version resource [at this time].
    angular之interceptors拦截器
  • 原文地址:https://www.cnblogs.com/nikyxxx/p/2240247.html
Copyright © 2020-2023  润新知