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>
- Xceed Datagrid for Wpf中带有可编辑combox项做法实例
- WPF数据表格控件Xceed DataGrid for WPF详细介绍
- WPF 中combox与datagrid绑定
- WPF之MVVM中DataGrid中嵌入Combox,改变Combox可回传至绑定的实体
- Xceed WPF 3D显示控件Xceed 3D Views for WPF控件详细介绍使用及下载
- Xceed dataGrid scroll FixedColumnCount
- Xceed DataGrid 中设置增加行的方法
- 【flex4】datagrid中使用combox
- EasyUI datagrid可编辑
- DataGrid control for WPF
- QT中使用Qcombox和QCompleter实现可编辑的combox下拉提示,并设置样式
- 在可编辑datagrid中,使用my97日期控件
- ext combox tree 实例 可运行 php
- DataGrid中的子控件Combox之数据源绑定(WPF)
- EasyUI DataGrid可编辑单元格
- EasyUI datagrid可编辑行
- easyui datagrid editgrid 可编辑datagrid(8)
- wpf实现可编辑listview
- chmod & chown
- js零碎知识点
- Boost.Lambda
- java实现二叉树的常见操作
- JIT File description
- Xceed Datagrid for Wpf中带有可编辑combox项做法实例
- quartz定时器格式设置
- Orcal中的SQL语句
- 毕业后续
- win 7和Ubuntu 12.04 双系统下的磁盘分区问题
- JAVA公私钥加解密程序
- 设计模式之桥接模式
- instantclient_11_2 和PL/SQL Developer连接oracle 11g远程数据库
- 用WMI实现Windows系统自动管理(多图)