开发者

How can I bind data to a ComboBox inside from DataGrid?

This is the user control which I'm working on. The items in the first comboBox are gonna be the same in dataGridColumn GroupID.

How can I bind data to a ComboBox inside from DataGrid?

The code to show in the first comboBox is

    <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;
    }

But in the another comboBox, don't appear any item in it.

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

How can I bind it?

I was thinking in put all the Groups in a list, but I'm not sure it'll be a good way because I'd need to convert my query in a list.

UPDATE 1:

I had to r开发者_开发百科emove this line:

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

for

            <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>


The binding of your internal ComboBox will not work because the DataContext of that ComboBox is your Groups collection but one of the Students, this means setting the ItemsSource to {Binding} will make the Student the ItemsSource.

You can navigate up the Tree with a RelativeSource binding to a place where the inherited DataContext is still the Groups collection, e.g. if the inheritance is not blocked by setting the DataContext inbetween this will work:

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


Create a Group class and in that class include a public List Students. I don't have the same names but I am doing functionally what (I think) you are asking. In this example each workflow as multiple batches. The second list box binds to the selected item in WFEnum. You need to use SelectedItem.Students to get the Student collection of the selected Group. This is from working production code. ElementName is what tells it to bind to the control.

    <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"/>

Hope this helps the class Workflow. Notice the WorkFlow has a public list Batches (for you students). Notice in the case I only get the students from SQL on demand but once I get them I save the list and next time don't go back to SQL. WPF and data binding is kind of complex but it is cool.

    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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜