是否可以使用CheckBox和TextBlock编辑单元格

时间:2016-02-04 07:44:14

标签: c# wpf xaml checkbox datagrid

我有以下编码,我将CheckBox和TextBlock绑定到一个DataGridTemplateColumn

当我单击单元格本身编辑其中的文本时,是否可以使用复选框和文本框编辑单元格?我仍然希望能够在编辑文本块中的文本的同时将我的CheckBox设置为true或false。

这是我的编码:

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

我现在看起来如何以及如何在单元格内编辑R12值的示例,同时仍然可以将复选框设置为true或false。

enter image description here

2 个答案:

答案 0 :(得分:0)

为什么要使用TextBlock代替TextBox?如果要扩展列长的整个宽度,请将HorizontalAlignment设置为Stretch,如下所示:

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

<强>更新

TextBox放入GridDockPanel; 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.

答案 1 :(得分:0)

至于我的原始问题,是的,您可以使用CheckBox内部编辑单元格,但我使用TextBlock而不是TextBox我改变了我的以下编码问题:

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

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

由于StackPanel不支持某些元素(例如TextBox)来填充剩余的可用空间,DockPanel确实支持它。

然后我添加了这一行,让我的TextBox填充剩余的可用空间

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

希望这会帮助其他人:)