您好我实际上正在创建一个WPF DataGrid自定义控件。 什么是这个组合框应该显示组合框中的性别和什么是当我保持组合框外的数据模板的工作,但在它不工作的数据模板内部发生了什么。请帮帮我 ?数据块中的组合框绑定
<UserControl x:Class="Custom_DataGrid.Grid3.Grid3"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" xmlns:sdk="http://schemas.microsoft.com/wpf/2008/toolkit"
d:DesignHeight="300" d:DesignWidth="300" >
<UserControl.Resources>
<DataTemplate x:Key="dueDateCellTemplate">
<TextBlock Text="{Binding DOB}" Margin="5,4,5,4"/>
</DataTemplate>
<DataTemplate x:Key="dueDateCellEditingTemplate">
<DatePicker SelectedDate="{Binding DOB, Mode=TwoWay}"/>
</DataTemplate>
<DataTemplate x:Key="genderCellTemplate">
<TextBlock Text="{Binding GENDER.Gender}" Margin="5,4,5,4"/>
</DataTemplate>
<DataTemplate x:Key="genderCellEditingTemplate">
<!--<ComboBox ItemsSource="{Binding Genders}" SelectedItem="{Binding Genders}" SelectedValue="{Binding Path=GENDER, ElementName=Id}" DisplayMemberPath="Id" SelectedValuePath="Gender"></ComboBox>-->
<ComboBox x:Name="c1" ItemsSource="{Binding Genders}" DisplayMemberPath="Id" Height="50" Width="100"></ComboBox>
<!--<TextBlock Text="{Binding Genders.Gender}" Foreground="Khaki"></TextBlock>-->
</DataTemplate>
</UserControl.Resources>
<Grid>
<DataGrid x:Name="datagrid3" AutoGeneratingColumn="datagrid3_AutoGeneratingColumn" ItemsSource="{Binding Employees}" Foreground="SteelBlue"></DataGrid>
</Grid>
</UserControl>
public enum Designation
{
Sales, Development, HR, BackOffice
}
public class Sex
{
private string id;
private string gen;
public string Id
{
get
{
return id;
}
set
{
id = value;
}
}
public string Gender
{
get
{
return gen;
}
set
{
gen = value;
}
}
}
public class DataSource
{
ObservableCollection<Employee> empList = new ObservableCollection<Employee>();
List<Sex> genders = new List<Sex>();
public DataSource()
{
empList.Add(new Employee() { ID = 1, NAME = "Neeraj", DOB = Convert.ToDateTime("12/03/1986"), EMAIL = "[email protected]", GENDER = new Sex() { Id = "M", Gender = "Male" }, PHONE = "9999999999", ACTIVE = false, DESIGNATION = Designation.Development });
empList.Add(new Employee() { ID = 2, NAME = "Mayank", DOB = Convert.ToDateTime("01/01/1986"), EMAIL = "[email protected]", GENDER = new Sex() { Id = "M", Gender = "Male" }, PHONE = "9999999999", ACTIVE = true, DESIGNATION = Designation.BackOffice });
empList.Add(new Employee() { ID = 1, NAME = "Neeraj", DOB = Convert.ToDateTime("12/03/1986"), EMAIL = "[email protected]", GENDER = new Sex() { Id = "M", Gender = "Male" }, PHONE = "9999999999", ACTIVE = false, DESIGNATION = Designation.Development });
empList.Add(new Employee() { ID = 2, NAME = "Mayank", DOB = Convert.ToDateTime("01/01/1986"), EMAIL = "[email protected]", GENDER = new Sex() { Id = "M", Gender = "Male" }, PHONE = "9999999999", ACTIVE = true, DESIGNATION = Designation.BackOffice });
empList.Add(new Employee() { ID = 1, NAME = "Neeraj", DOB = Convert.ToDateTime("12/03/1986"), EMAIL = "[email protected]", GENDER = new Sex() { Id = "M", Gender = "Male" }, PHONE = "9999999999", ACTIVE = false, DESIGNATION = Designation.Development });
empList.Add(new Employee() { ID = 2, NAME = "Mayank", DOB = Convert.ToDateTime("01/01/1986"), EMAIL = "[email protected]", GENDER = new Sex() { Id = "M", Gender = "Male" }, PHONE = "9999999999", ACTIVE = true, DESIGNATION = Designation.BackOffice });
genders.Add(new Sex() { Id = "M", Gender = "Male" });
genders.Add(new Sex() { Id = "F", Gender = "Female" });
}
public ObservableCollection<Employee> Employees
{
get
{
return empList;
}
set
{
empList = value;
}
}
public List<Sex> Genders
{
get
{
return genders;
}
set
{
genders = value;
}
}
}
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 Custom_DataGrid.Grid3
{
/// <summary>
/// Interaction logic for Grid3.xaml
/// </summary>
public partial class Grid3 : UserControl
{
public Grid3()
{
InitializeComponent();
this.DataContext = new DataSource();
}
private void datagrid3_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
if (e.PropertyName.ToLower().ToString().Equals("id"))
{
e.Column.Header = "Employee Id";
}
else if (e.PropertyName.ToLower().Equals("name"))
{
e.Column.Header = "Employee Name";
}
else if (e.PropertyName.ToLower().Equals("dob"))
{
e.Column.Header = "Employee DOB";
if (e.PropertyType == typeof(DateTime))
{
DataGridTemplateColumn templateColumn = new DataGridTemplateColumn();
templateColumn.Header = "Employee DOB";
templateColumn.CellTemplate = (DataTemplate)Resources["dueDateCellTemplate"];
templateColumn.CellEditingTemplate = (DataTemplate)Resources["dueDateCellEditingTemplate"];
templateColumn.SortMemberPath = "DueDate";
e.Column = templateColumn;
}
}
else if (e.PropertyName.ToLower().Equals("phone"))
{
e.Column.Header = "Employee Phone";
e.Cancel = true;
}
else if (e.PropertyName.ToLower().Equals("email"))
{
e.Column.Header = "Employee Email";
}
else if (e.PropertyName.ToLower().Equals("gender"))
{
e.Column.Header = "Employee Gender";
DataGridTemplateColumn templateColumn = new DataGridTemplateColumn();
templateColumn.Header = "Employee Gender";
templateColumn.CellTemplate = (DataTemplate)Resources["genderCellTemplate"];
templateColumn.CellEditingTemplate = (DataTemplate)Resources["genderCellEditingTemplate"];
e.Column = templateColumn;
}
else if (e.PropertyName.ToLower().Equals("active"))
{
e.Column.Header = "Employee Active/InActive";
}
else if (e.PropertyName.ToLower().Equals("designation"))
{
e.Column.Header = "Employee Designation";
}
}
}
}
,所以我想在每一个事情工作正常的celleditingtemplate的combobxo创建模板列,但里面的DataTemplate它不是加工。 请帮帮我吗?检查文本框,我已评论它工作正常...
我怀疑这是否适用于DataGrid单元格中的DataTemplate。进一步'性别'不是UserControl本身的属性。编辑:抓住第一部分,如果你去根目录可能实际上工作,但路径可能应该是'DataContext.Genders'。 – 2011-04-29 17:52:59
是的,很好的接收..我几乎错过了.. ..!我没有使用'x:Reference',所以不知道它是如何工作的。 – 2011-04-29 18:06:59
其实我很惊讶这个作品,如果你试图找到DataGrid并绑定到它的DataContext(它应该是相同的) 。 – 2011-04-29 18:38:52