2011-08-15 52 views
3

这是我正在处理的用户控件。 dataGridColumn GroupID中的第一个组合框中的项目将会相同。如何将数据绑定到DataGrid内部的ComboBox?

enter image description here

的代码在第一组合框显示是

<ComboBox ItemsSource="{Binding}" Name="GroupComboBox" SelectedValuePath="GroupID" DisplayMemberPath="GroupName" Grid.Column="1" Margin="5" /> 

    private void LoadGroups() 
    { 
     NorthwindDataContext dc = new NorthwindDataContext(); 

     var groups = (from p in dc.Group 
         select p); 

     this.DataContext = groups; 
    } 

    private void LoadStudents() 
    { 
     NorthwindDataContext dc = new NorthwindDataContext(); 

     var students = (from p in dc.Student 
         select p); 

     dataGrid1.ItemsSource = students; 
    } 

但在另一个组合框,不要在它出现的任何项目。

  <DataGridTemplateColumn Header="GroupID"> 
       <DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <ComboBox ItemsSource="{Binding}" SelectedValuePath="GroupID" DisplayMemberPath="GroupName" /> 
        </DataTemplate> 
       </DataGridTemplateColumn.CellTemplate> 
      </DataGridTemplateColumn> 

我该如何绑定它?

我在想把所有的组放在一个列表中,但我不确定它会是一个好方法,因为我需要将列表中的查询转换成列表。

更新1:

,我不得不删除这条线:

  <DataGridComboBoxColumn Header="GroupID" ItemsSource="{Binding DataContext, RelativeSource={RelativeSource AncestorType=UserControl}}" SelectedValuePath="GroupID" DisplayMemberPath="GroupName" /> 

  <DataGridTemplateColumn Header="GroupID"> 
       <DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <ComboBox ItemsSource="{Binding DataContext, RelativeSource={RelativeSource AncestorType=UserControl}}" SelectedValuePath="GroupID" DisplayMemberPath="GroupName" SelectedValue="{Binding GroupID}" /> 
        </DataTemplate> 
       </DataGridTemplateColumn.CellTemplate> 
      </DataGridTemplateColumn> 
+0

我会建议使用WPF重新标记为不同的UI是非常不同的,当涉及到结合。谢谢。 – Jay

+0

编辑此帖子上的标签以包含WPF,您可能还想删除数据库和/或linq。恰当地标记你的问题对于吸引正确的专家到你的文章非常重要。 – Jay

+0

Ohh没错,我忘了最重要的标签 –

回答

1

不会起作用,因为该组合框的DataContext的是你的Groups集,但Students之一,这意味着设置ItemsSource{Binding}将使StudentItemsSource的内部组合框的结合。

您可以使用RelativeSource绑定到继承的DataContext仍然是Groups集合(例如,如果inheritance不会被阻止通过设置DataContext其间这将工作:

{Binding DataContext, RelativeSource={RelativeSource AncestorType=DataGrid}} 
+0

它还没有工作。第一个组合框是好的,但另一个不是。你认为我做错了吗? this.DataContext = groups; –

+0

@ oscar.imbres:是的,但我的约束旨在说明这一点。您是否在UserControl和DataGrid之间的树中的任何点设置了DataContext? –

+0

@ oscar.imbres:您可以将'AncestorType'更改为'UserControl',它将导航到顶端并在那里获得'this.DataContext',但这可能会更强大一点。 (之所以说'this.DataContext'是错误的是因为你通常希望UserControls能够从外部继承te DataContext。如果你设置它不起作用) –

0

创建一个组类,并在该类包括公开名单的学生。我没有相同的名字,但我正在功能上做什么(我认为)你问。在这个例子中,每个工作流程都是多个批次。第二个列表框绑定到WFEnum中的选定项目。您需要使用SelectedItem.Students获取所选组的学生集合。这是来自正在运行的生产代码。 ElementName是告诉它绑定到控件的东西。

<ListBox Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="1" Name="WFEnum" 
      ItemsSource="{Binding Path=SearchItem.SrchWorkFlows}" 
      DisplayMemberPath="Name"}"> 
    ... 
    <ListBox Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="1" Name="WFBatchEnum" 
      ItemsSource="{Binding SelectedItem.Batches, ElementName=WFEnum}" 
      DisplayMemberPath="BatchName" SelectedValuePath="ID" 
      HorizontalAlignment="Left"/> 

希望这有助于工作流程。注意WorkFlow有一个公开清单批次(为你的学生)。注意,在我只按需求从SQL获得学生的情况下,但是一旦我得到它们,我保存该列表,下次不回SQL。 WPF和数据绑定有点复杂,但很酷。

public class Workflow : INotifyPropertyChanged 
    { 
     private Int16 id; 
     private string name; 
     private string description;  
     private Boolean active; 
     private User createdBy; 
     private DateTime createDate; 
     private List<WFBatch> batches = new List<WFBatch>(); 

     public event PropertyChangedEventHandler PropertyChanged; 
     protected void NotifyPropertyChanged(String info) 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(info)); 
      } 
     } 

     public Int16 ID { get { return id; } } 
     public string Name { get { return name; } } 
     public string Description { get { return description; } }   
     public Boolean Active { get { return active; } } 
     public User CreatedBy { get { return createdBy; } } 
     public DateTime CreateDate { get { return createDate; } } 
     public WFBatch newWFbatch { get { return new WFBatch(this, App.StaticGabeLib.Search.IncludeFamilySrched); } } 
     public List<WFBatch> Batches 
     { 
      get 
      { 
       // get the latest from SQL 
       // todo optimize - this for now will a SQL call for all batches here 
       if (batches.Count > 0) return batches; 
       SqlConnection sqlConnRW2 = new SqlConnection(sqlConnStringLibDef); 
       try 
       { 
        sqlConnRW2.Open(); 
        SqlCommand sqlCmd2 = new SqlCommand(); 
        sqlCmd2.Connection = sqlConnRW2; 
        SqlDataReader rdr; 
        sqlCmd2.CommandText = sqlCmd2.CommandText = "Select [ID] from [wfBch] with (nolock) " + Environment.NewLine + 
         "where [wfID] = '" + ID.ToString() + "' order by [ID] "; 
        rdr = sqlCmd2.ExecuteReader(); 
        while (rdr.Read()) 
        { 
         batches.Add(new WFBatch(this, rdr.GetInt16(0))); 
        } 
       } 
       catch (Exception Ex) 
       { 
        Debug.WriteLine(Ex.Message); 
       } 
       finally 
       { 
        sqlConnRW2.Close(); 
       } 
       return batches; 
      } 
     } 
     public Workflow(Int16 ID, string Name, string Description, Boolean Active, User CreatedBy, DateTime CreateDate) 
     { 
      id = ID; 
      name = Name; 
      description = Description; 
      active = Active; 
      createdBy = CreatedBy; 
      createDate = CreateDate; 
      // WFBatch wfBatch = new WFBatch(this); 
      // todo retreive batches 
     }   
    } 
+0

我试图得到它。对不起,我仍然是WPF的新手 –

相关问题