WPF Datagrid模板列编辑事件和交替列颜色 [英] WPF Datagrid Template column edit event and alternating column color
问题描述
我正在开发的应用程序是高度动态的,所以列数是未知。所以我正在从后面的代码创建DataGridTemplate列。
问题1 :我希望交替列具有不同的背景颜色。如何做程序化? (DataGridTemplateColumn没有Background属性,所以我无法找出解决方案)
问题2 :我的DataGridTemplateColumn有一个DataTemplate,其中我有一个StackPanel,其中有2个TextBox。 DataGrid中有一个叫做CellEditing Event的事件,当我们编辑一个单元格时,它会被触发。它适用于默认列,但对于我的列,如果我编辑这些文本框,事件是snot被触发!那么我该如何实现呢?
(我有时会被WPF惊讶!!!)
问题零如果您使用 AutoGenerateColumns =true,您可以为您生成一个datagrid中的列
当你设置你的datagrid。它不会动态添加列,但是如果您重置项目源代码,它可能会有(不是那个)
问题一 DataGrid具有属性 AlternatingRowBackground
和 AlternationCount
设置交替行背景。但是我并没有看到任何交替的列背景在网格本身。您可以在数据表中进行操作:
< DataGridTemplateColumn.CellTemplate>
< DataTemplate>
< TextBlock Background =RedForeground =White>我是Red< / TextBlock>
< / DataTemplate>
< /DataGridTemplateColumn.CellTemplate>
但是我仍然看到一个边距,即使margin = 0,所以细胞看起来很滑稽,如果你使用任何非常明显的颜色。
问题二您是否意味着 CellEndEditing
事件?因为我没有看到任何其他单元格编辑事件。我尝试了以下内容:
< DataGrid ItemsSource ={Binding}AutoGenerateColumns =FalseCellEditEnding =DataGrid_CellEditEnding> ;
< DataGrid.Columns>
< DataGridTextColumn Header =ABinding ={Binding Field0}/>
< DataGridTemplateColumn Header =BC>
< DataGridTemplateColumn.CellTemplate>
< DataTemplate>
< StackPanel Orientation =Horizontal>
< TextBlock Text ={Binding Field1}/>
< TextBlock Text ={Binding Field2}/>
< / StackPanel>
< / DataTemplate>
< /DataGridTemplateColumn.CellTemplate>
< DataGridTemplateColumn.CellEditingTemplate>
< DataTemplate>
< StackPanel Orientation =Horizontal>
< TextBox Text ={Binding Field1}/>
< TextBox Text ={Binding Field2}/>
< / StackPanel>
< / DataTemplate>
< /DataGridTemplateColumn.CellEditingTemplate>
< / DataGridTemplateColumn>
< /DataGrid.Columns>
< / DataGrid>
我的 DataGrid_CellEditEnding
CellEditingTemplate中的任何一个文本框都会失去焦点,无论数据是否改变,所以事情似乎对我来说是有效的。您是否使用其他DataGrid而不是内置WPF?
I have been using WPF for quiet sometime. I know DataGrid in WPF does not have a Column collection as dependency property so columns cannot be added dynamically.
The application I am developing is highly dynamic so the number of columns are not known. So I am creating DataGridTemplate columns from code behind.
Problem 1 : I want alternating columns to have different background color. How do I do it programatically?? (DataGridTemplateColumn doesnot have a Background property so I am not able to figure out a solution)
Problem 2 : My DataGridTemplateColumn has a DataTemplate in which I have a StackPanel with 2 TextBoxes in it. There is an event in DataGrid called CellEditing Event which fires when we edit a cell. It works for default column, but for my column if I edit those TextBoxes, the event is snot getting fired!!! So how do I achieve it??
(I sometimes get amazed by WPF !!!)
Problem Zero You can have the columns in a datagrid generated for you if you use AutoGenerateColumns="true"
when you set up your datagrid. It won't add columns dynamically later, but it might if you reset the itemssource? (not positive on that one)
Problem One DataGrid has properties AlternatingRowBackground
and AlternationCount
to set up alternating row backgrounds. But i don't see anything for alternating column backgrounds in the grid itself. You could do it inside your datatemplate, though:
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Background="Red" Foreground="White">I'm Red</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
But i still see a margin inside that even with margin=0, so the cells look funny if you use any really obvious colors.
Problem Two do you mean the CellEndEditing
event? Because i don't see any other cell editing events. I tried the following:
<DataGrid ItemsSource="{Binding}" AutoGenerateColumns="False" CellEditEnding="DataGrid_CellEditEnding">
<DataGrid.Columns>
<DataGridTextColumn Header="A" Binding="{Binding Field0}" />
<DataGridTemplateColumn Header="BC">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Field1}"/>
<TextBlock Text="{Binding Field2}" />
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBox Text="{Binding Field1}"/>
<TextBox Text="{Binding Field2}" />
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
And my DataGrid_CellEditEnding
event handler gets called whenever either of the textboxes in the CellEditingTemplate lose focus, whether data changed or not, so things appear to be working for me.
Are you using some other DataGrid than the "built in" WPF one?
这篇关于WPF Datagrid模板列编辑事件和交替列颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!