2011-04-29 82 views
0

您好我實際上正在創建一個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它不是加工。 請幫幫我嗎?檢查文本框,我已評論它工作正常...

回答

3

如果你是在一個DataTemplate中的DataContext將被模板化的對象。因此,如果僅指定了綁定路徑,則相對於DataContext的綁定將找不到ItemsSource。

通常情況下,您可以使用RelativeSource -binding來查找仍具有可找到您的ItemsSource的DataContext的控件。 (請參閱RV1987的答案;我認爲它沒有提前工作,因爲如果您有DataGridComboBoxColumn,那麼相同的RelativeSource-ItemsSource綁定將會不工作,這是因爲一個列本身是抽象的並且不出現在樹中不像是一個模板

由於用戶控件的DataContext的創建應該是你在找什麼,你能說出你的用戶控件(control爲例)並結合這樣的控制:

ItemsSource="{Binding Source={x:Reference control}, Path=DataContext.Genders}" 

(請注意,x:Reference是相當新的,它不存在在.NET 3.5,使用ElementName=control反而會工作)

0

嘗試在你的DataTemplate使用此 -

<ComboBox x:Name="c1" ItemsSource="{Binding DataContext.Genders, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" DisplayMemberPath="Id" Height="50" Width="100"></ComboBox> 

這會找到你的用戶控件的DataContext的是數據源的財產性別。

+0

我懷疑這是否適用於DataGrid單元格中的DataTemplate。進一步'性別'不是UserControl本身的屬性。編輯:抓住第一部分,如果你去根目錄可能實際上工作,但路徑可能應該是'DataContext.Genders'。 – 2011-04-29 17:52:59

+0

是的,很好的接收..我幾乎錯過了.. ..!我沒有使用'x:Reference',所以不知道它是如何工作的。 – 2011-04-29 18:06:59

+0

其實我很驚訝這個作品,如果你試圖找到DataGrid並綁定到它的DataContext(它應該是相同的) 。 – 2011-04-29 18:38:52