是否有可能使用复选框和文本块编辑单元格
是否有可能使用复选框和文本块编辑单元格
我有以下代码,在一个DataGridTemplateColumn
中绑定了一个复选框和文本框。当我点击单元格本身来编辑其中的文本时,是否可能同时编辑具有复选框和文本框的单元格?我仍然希望能够在编辑文本块内的文本的同时将复选框设置为true或false。以下是我的代码:
private void btnFeedbackSelectSupplier_Click(object sender, RoutedEventArgs e) { DataGridTemplateColumn columnFeedbackSupplier = new DataGridTemplateColumn(); columnFeedbackSupplier.Header = "供应商"; columnFeedbackSupplier.CanUserReorder = true; columnFeedbackSupplier.CanUserResize = true; columnFeedbackSupplier.IsReadOnly = false; //我将两个元素放在这里的堆栈面板中 var stackPanel = new FrameworkElementFactory(typeof(StackPanel)); stackPanel.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal); DataTemplate cellTemplate = new DataTemplate(); //创建复选框的位置 FrameworkElementFactory factoryCheck = new FrameworkElementFactory(typeof(CheckBox)); Binding bindCheck = new Binding("TrueFalse"); bindCheck.Mode = BindingMode.TwoWay; factoryCheck.SetValue(CheckBox.IsCheckedProperty, bindCheck); stackPanel.AppendChild(factoryCheck); //创建文本框的位置 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; IEnumerableitems = list.Cast (); 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。
问题的出现原因是希望在单元格中使用CheckBox和TextBlock进行编辑,但是想要使用TextBlock而不是TextBox。想要使TextBlock扩展到整个列的宽度,可以将HorizontalAlignment属性设置为Stretch。
解决方法是使用代码创建一个FrameworkElementFactory对象,将其类型设置为TextBox,并将Text属性设置为HorizontalAlignment.Stretch。然后将TextBox放入Grid或DockPanel中。 StackPanel无法填充剩余可用空间,因此需要将StackPanel更改为DockPanel,并使用factoryText.SetValue(TextBox.HorizontalAlignmentProperty, HorizontalAlignment.Stretch)来设置TextBox的HorizontalAlignment属性。
注意:作者表示未对解决方案进行测试,对答案进行了编辑。
以下是整理后的
为什么要使用TextBlock而不是TextBox?如果想要使其扩展到整个列的宽度,只需将HorizontalAlignment属性设置为Stretch,代码如下:
FrameworkElementFactory factoryText = new FrameworkElementFactory(typeof(TextBox)); factoryText.Text = HorizontalAlignment.Stretch;
更新:将TextBox放入Grid或DockPanel中,根据Zach Johnson的说法,StackPanel用于“堆叠”元素,即使在可见区域之外,也不允许填充剩余的空间。可以将StackPanel更改为DockPanel或Grid。
但是需要注意,上述解决方案中的代码有误,正确的写法应该是:
factoryText.SetValue(TextBox.HorizontalAlignmentProperty, HorizontalAlignment.Stretch);
并将StackPanel更改为DockPanel。
抱歉作者没有测试解决方案,已对答案进行了编辑。
原因:原问题是关于在一个单元格中同时使用CheckBox和TextBlock编辑的问题。原作者尝试使用StackPanel作为单元格的容器,并在其中放置一个CheckBox和一个TextBlock。然而,StackPanel不支持一些元素(比如TextBox)填充剩余的可用空间,导致无法同时编辑CheckBox和TextBlock。
解决方法:原作者改用DockPanel作为单元格的容器,并将CheckBox和TextBox放置在其中。DockPanel可以支持元素填充剩余的可用空间。作者还通过设置TextBox的HorizontalAlignment属性为Stretch来使其填充剩余空间。
以下是代码示例:
var dockPanel = new FrameworkElementFactory(typeof(DockPanel)); factoryText.SetValue(TextBox.HorizontalAlignmentProperty, HorizontalAlignment.Stretch);
希望这个解决方法能帮助到其他遇到类似问题的人。对于之前没有测试答案的问题,作者已经进行了修改。