jeudi 4 février 2016

Is it possible to edit a cell with a CheckBox and TextBlock

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




Aucun commentaire:

Enregistrer un commentaire