【问题标题】:Is it possible to edit a cell with a CheckBox and TextBlock是否可以使用 CheckBox 和 TextBlock 编辑单元格
【发布时间】:2016-05-13 17:21:26
【问题描述】:

我有以下编码,我将 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。

【问题讨论】:

  • 为什么不用TextBox 而不是TextBlock
  • 我知道有人会问这个问题:P 因为TextBox 看起来很傻,因为它不想扩展我的列长度的整个宽度
  • 只需设置HorizontalAlignment="Stretch" 的属性TextBox
  • factoryText.Text = HorizontalAlignment.Stretch; 喜欢这样吗? 0_o
  • 如果factoryTextTextBox,那么textBox.HorizontalAlignment = HorizontalAlignment.Stretch;

标签: c# wpf xaml checkbox datagrid


【解决方案1】:

为什么要使用 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.

【讨论】:

  • 您的答案将不起作用,因为您必须这样做:factoryText.SetValue(TextBox.HorizontalAlignmentProperty, HorizontalAlignment.Stretch); 并将StackPanel 更改为DockPanel,因为 StackPanel 不支持像 TextBox 这样的元素来填充剩余空间;)
  • @CareTaker22 抱歉,我没有测试我的解决方案。我已经编辑了我的答案。
【解决方案2】:

至于我最初的问题,是的,您可以编辑带有CheckBox 的单元格,但我使用的是TextBox 而不是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);

希望这会帮助其他人:)

【讨论】:

  • 对不起,我没有测试我的答案。我已经编辑了我的答案:)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-12
  • 2019-09-01
  • 1970-01-01
  • 2021-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多