Xceed Datagrid for Wpf中带有可编辑combox项做法实例

来源:互联网 发布:大数据产生的背影 题 编辑:程序博客网 时间:2024/04/26 07:39

Xceed Datagrid for Wpf中带有可编辑combox项做法实例(并且关联外键)。首先前台xaml中显示字段中需要加CellContentTemplate,CellEditor两个属性并对这两个属性编辑相应的代码即ComboBoxEditors.xaml文件所示。


MainWindow.xaml:

<Window x:Class="Xceed.Wpf.DataGrid.ForenKeyData.Edit.MainWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        xmlns:local="clr-namespace:Xceed.Wpf.DataGrid.ForenKeyData.Edit"        Title="MainWindow" Height="350" Width="525" xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid">    <Window.Resources>        <ResourceDictionary>            <ResourceDictionary.MergedDictionaries>                <ResourceDictionary Source="ComboBoxEditors.xaml" />            </ResourceDictionary.MergedDictionaries>        </ResourceDictionary>    </Window.Resources>    <Grid>        <xcdg:DataGridControl Name="grid"                              ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType=local:MainWindow},Path=Values}"                              AutoCreateForeignKeyConfigurations="True">            <xcdg:DataGridControl.Columns>                <xcdg:Column FieldName="id" Title="ID"/>                <xcdg:Column FieldName="name" Title="Name" />                <xcdg:Column FieldName="AddressId"                             Title="ComboBox"                             Width="125"                             CellContentTemplate="{StaticResource categoryCellDataTemplate}"                                     CellEditor="{StaticResource categoryEditor}"                             Visible="{Binding Source={x:Static local:MainPageParams.Singleton},Path=ComboBoxColumnsVisible}"                         />            </xcdg:DataGridControl.Columns>        </xcdg:DataGridControl>    </Grid></Window>


MainWindow.xaml.cs:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;namespace Xceed.Wpf.DataGrid.ForenKeyData.Edit{    /// <summary>    /// MainWindow.xaml 的交互逻辑    /// </summary>    public partial class MainWindow : Window    {        public MainWindow()        {            InitializeComponent();            Address adr1 = new Address() { id = 1, adrName = "上海" };            Address adr2 = new Address() { id = 2, adrName = "无锡" };            Address adr3 = new Address() { id = 3, adrName = "南京" };            Address adr4 = new Address() { id = 4, adrName = "徐州" };            Address adr5 = new Address() { id = 5, adrName = "北京" };            DepartmentsList = new List<Address>();            DepartmentsList.Add(adr1);            DepartmentsList.Add(adr2);            DepartmentsList.Add(adr3);            DepartmentsList.Add(adr4);            DepartmentsList.Add(adr5);            ValuesList = new List<Employee>();            ValuesList.Add(new Employee() { id = 1, name = "张三", AddressId = adr1 });            ValuesList.Add(new Employee() { id = 2, name = "李四", AddressId = adr2 });            ValuesList.Add(new Employee() { id = 3, name = "王五", AddressId = adr3 });            ValuesList.Add(new Employee() { id = 4, name = "赵六", AddressId = adr4 });            ValuesList.Add(new Employee() { id = 5, name = "田七", AddressId = adr5 });        }        private static List<Address> DepartmentsList;        private static List<Employee> ValuesList;        public List<Address> Departments        {            get            {                return MainWindow.DepartmentsList;            }        }        public List<Employee> Values        {            get            {                return MainWindow.ValuesList;            }        }    }    public class Address    {        public int id { get; set; }        public string adrName { get; set; }    }    public class Employee    {        public int id { get; set; }        public string name { get; set; }        public Address AddressId { get; set; }    }}


MainPageParams.cs:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.ComponentModel;namespace Xceed.Wpf.DataGrid.ForenKeyData.Edit{    class MainPageParams : INotifyPropertyChanged    {        public static readonly MainPageParams Singleton = new MainPageParams();        private MainPageParams()        {        }        private bool m_comboBoxColumnsVisible = true;        public bool ComboBoxColumnsVisible        {            get            {                return m_comboBoxColumnsVisible;            }            set            {                if (m_comboBoxColumnsVisible == value)                    return;                m_comboBoxColumnsVisible = value;                this.OnPropertyChanged(new PropertyChangedEventArgs("ComboBoxColumnsVisible"));            }        }        protected void OnPropertyChanged(PropertyChangedEventArgs e)        {            if (this.PropertyChanged != null)                this.PropertyChanged(this, e);        }        public event PropertyChangedEventHandler PropertyChanged;    }}


ComboBoxEditors.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"                    xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid"                    xmlns:local="clr-namespace:Xceed.Wpf.DataGrid.ForenKeyData.Edit">        <DataTemplate x:Key="categoryCellDataTemplate">        <TextBlock Text="{Binding adrName}"/>    </DataTemplate>    <!-- This is a very straightforward ComboBox editor used for the Category Column. -->    <xcdg:CellEditor x:Key="categoryEditor">        <xcdg:CellEditor.EditTemplate>            <DataTemplate>                <ComboBox BorderThickness="0"                      VerticalContentAlignment="Top"                      MinHeight="22"                      ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType=local:MainWindow},Path=Departments}"                      SelectedItem="{xcdg:CellEditorBinding}"                          DisplayMemberPath="adrName"                          SelectedValuePath="adrName"                      FocusVisualStyle="{x:Null}">                    <ComboBox.Resources>                        <Style TargetType="Popup">                            <Setter Property="TextElement.Foreground"                             Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}" />                        </Style>                    </ComboBox.Resources>                </ComboBox>            </DataTemplate>        </xcdg:CellEditor.EditTemplate>        <!-- Declare the wanted key gesture that will activate the ComboBox CellEditor. -->        <xcdg:CellEditor.ActivationGestures>            <xcdg:KeyActivationGesture SystemKey="Down"                                    Modifiers="Alt" />            <xcdg:KeyActivationGesture Key="Up"                                    Modifiers="Alt" />            <xcdg:KeyActivationGesture Key="F4" />            <xcdg:KeyActivationGesture Key="Space" />        </xcdg:CellEditor.ActivationGestures>    </xcdg:CellEditor></ResourceDictionary>