0

I have the following coding where I bind a CheckBox and TextBlock into one DataGridTemplateColumn.

Would it be possible for me to edit the cell with the checkbox and textbox when I click on the cell itself to edit the text inside of it? I still want to be able to set my CheckBox to true or false at the same time as editing the text within the textblock.

Here is my coding:

  private void btnFeedbackSelectSupplier_Click(object sender, RoutedEventArgs e)
    { 
        DataGridTemplateColumn columnFeedbackSupplier = new DataGridTemplateColumn();
        columnFeedbackSupplier.Header = "Supplier";
        columnFeedbackSupplier.CanUserReorder = true;
        columnFeedbackSupplier.CanUserResize = true;
        columnFeedbackSupplier.IsReadOnly = false;

        //My stack panel where I will host the two elements 
        var stackPanel = new FrameworkElementFactory(typeof(StackPanel));
        stackPanel.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);

        DataTemplate cellTemplate = new DataTemplate();

        //Where I create my checkbox
        FrameworkElementFactory factoryCheck = new FrameworkElementFactory(typeof(CheckBox));
        Binding bindCheck = new Binding("TrueFalse");
        bindCheck.Mode = BindingMode.TwoWay;
        factoryCheck.SetValue(CheckBox.IsCheckedProperty, bindCheck);
        stackPanel.AppendChild(factoryCheck);

        //Where I create my textblock
        FrameworkElementFactory factoryText = new FrameworkElementFactory(typeof(TextBlock));
        Binding bindText = new Binding("Supplier");
        bindText.Mode = BindingMode.TwoWay;
        factoryText.SetValue(TextBlock.TextProperty, bindText);
        stackPanel.AppendChild(factoryText);

        cellTemplate.VisualTree = stackPanel;
        columnFeedbackSupplier.CellTemplate = cellTemplate;

        DataGridTextColumn columnFeedbackSupplierItem = new DataGridTextColumn();
        columnFeedbackSupplier.Header = (cmbFeedbackSelectSupplier.SelectedItem as DisplayItems).Name;

        dgFeedbackAddCost.SelectAll();

        IList list = dgFeedbackAddCost.SelectedItems as IList;
        IEnumerable<ViewQuoteItemList> items = list.Cast<ViewQuoteItemList>();

        var collection = (from i in items
                          let a = new ViewQuoteItemList { Item = i.Item, Supplier = i.Cost, TrueFalse = false }
                          select a).ToList();

        dgFeedbackSelectSupplier.Columns.Add(columnFeedbackSupplier);
        dgFeedbackSelectSupplier.ItemsSource = collection;
    }

My example of how it looks now and how I would like to edit that R12 value inside the cell, while still being able to set the checkbox to true or false.

enter image description here

CareTaker22
  • 1,260
  • 3
  • 18
  • 36

2 Answers2

0

Why do you want to use TextBlock instead of TextBox ? If you want to expand the full width of my column length, then just set HorizontalAlignment to Stretch like that:

FrameworkElementFactory factoryText = new FrameworkElementFactory(typeof(TextBox));
factoryText.Text = HorizontalAlignment.Stretch;

Update:

And put your TextBox into Grid or DockPanel; as Zach Johnson says that StackPanel is meant for "stacking" things even outside the visible region, so it won't allow you to fill remaining space in the stacking dimension.

Community
  • 1
  • 1
StepUp
  • 36,391
  • 15
  • 88
  • 148
  • Your answer will not work as you have to do it like this: `factoryText.SetValue(TextBox.HorizontalAlignmentProperty, HorizontalAlignment.Stretch);` and change the `StackPanel` to a `DockPanel`, because the StackPanel does not support the element like the TextBox to fill the remaining space available ;) – CareTaker22 Feb 04 '16 at 19:21
  • @CareTaker22 sorry that I've not tested my solution. I've edited my answer. – StepUp Feb 05 '16 at 14:56
0

As for my original question, YES you can edit the cell with a CheckBox inside, but instead of a TextBlock I used a TextBox and I changed my the following coding from my question:

var stackPanel = new FrameworkElementFactory(typeof(StackPanel));
stackPanel.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);//Delete this line

To

var dockPanel = new FrameworkElementFactory(typeof(DockPanel));

Because a StackPanel does not have support for certain elements (like a TextBox) to fill the remaining available space, where a DockPanel does have support for it.

And then I added this line to make my TextBox fill the remaining space availble

factoryText.SetValue(TextBox.HorizontalAlignmentProperty, HorizontalAlignment.Stretch);

Hope this will help someone else out there :)

CareTaker22
  • 1,260
  • 3
  • 18
  • 36