zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    非域,非匿名用户访问远程企业服务的详细步骤
    调用远程的企业服务的安全问题
    未能加载文件或程序集“****”或它的某一个依赖项的一种情况
    XAMPP使用非80端口的安装配置修改
    Lucene 的存储结构概述
    .NET Framework 4.0 SDK的安装
    lucene 文件存储相关的几个类
    ASP.NET 状态服务 及 session丢失问题解决方案总结
    不安装.net framework框架运行.Net 程序的方法<收藏>
    net面试题集及答案
  • 原文地址:https://www.cnblogs.com/nikyxxx/p/2240247.html
Copyright © 2011-2022 走看看