0

How can I bind a List Property inside DataGridTextColumn to bind a list from main window.

For this I use

public class DataGridListBoxColumn : DataGridTextColumn
    {
        TextBlock tx = new TextBlock();

        public DataGridListBoxColumn()
        {
            tx.Name = "TxB";
        }
        protected override FrameworkElement GenerateEditingElement(DataGridCell cell, Object dataItem)
        {
            Binding b = new Binding();
            b.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(DataGridListBoxColumn), 1);
            b.Path = new PropertyPath("ListItem");
            tx.SetBinding(TextBlock.TextProperty, b);
            return cell;
        }
        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            var x = ListItem;
            Debugger.Break(); //here x is null 
        }

        public List<Student> ListItem
        {
            get { return (List<Student>)GetValue(ItemSourceProperty); }
            set { SetValue(ItemSourceProperty, value); }
        }
        public static readonly  DependencyProperty ItemSourceProperty = DependencyProperty.Register("ListItem", typeof(List<Student>), typeof(DataGridListBoxColumn));

    }

I use TextBlock as hidden for binding purpose only.

How to pass a list binding to DataGridTextColumn?

James Z
  • 12,209
  • 10
  • 24
  • 44
babucr
  • 11
  • 5
  • 2
    Why are you using DataGridTextColumn? DataGridTemplateColumn with a suitable DataTemplate is the approach that seems best to me. Otherwise use a converter on the list binding. – AQuirky Jul 15 '18 at 16:14
  • ok suppose if i change to DataGridTemplateColumn then also how can i get list inside my column – babucr Jul 16 '18 at 04:22
  • You don't need to create a custom colum type. Use a DataGridTemplateColumn with a CellTemplate containing an ItemsControl that binds to an IEnumerable of your data objects. – mm8 Jul 16 '18 at 09:26
  • There are any number of ways to do this. What is not clear is what you want. Do you want a single row containing the entire list from the main window? So if the list is 5 items then the cell is 5 lines high? What if the list has 100 items? Do you want to match one item of the list to one row of the table? To get a decent answer you need to provide a clear example. The code you have provided is muddled and does provide many clues. Provide the data. Explain why you want to do this. – AQuirky Jul 17 '18 at 03:55

0 Answers0