Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF Datagrid模板列编辑事件和交替列颜色_Wpf_Datagrid_Background Color - Fatal编程技术网

WPF Datagrid模板列编辑事件和交替列颜色

WPF Datagrid模板列编辑事件和交替列颜色,wpf,datagrid,background-color,Wpf,Datagrid,Background Color,我一直在使用WPF安静的一段时间。我知道WPF中的DataGrid没有列集合作为依赖属性,因此无法动态添加列 我正在开发的应用程序是高度动态的,因此列数未知。因此,我从代码隐藏中创建DataGridTemplate列 问题1:我希望交替列具有不同的背景色。我如何通过编程来实现它??(DataGridTemplateColumn没有后台属性,因此我无法找到解决方案) 问题2:我的DataGridTemplateColumn有一个DataTemplate,其中我有一个StackPanel,其中有两个

我一直在使用WPF安静的一段时间。我知道WPF中的DataGrid没有列集合作为依赖属性,因此无法动态添加列

我正在开发的应用程序是高度动态的,因此列数未知。因此,我从代码隐藏中创建DataGridTemplate列

问题1:我希望交替列具有不同的背景色。我如何通过编程来实现它??(DataGridTemplateColumn没有后台属性,因此我无法找到解决方案)

问题2:我的DataGridTemplateColumn有一个DataTemplate,其中我有一个StackPanel,其中有两个文本框。DataGrid中有一个名为CellEditing event的事件,当我们编辑单元格时会触发该事件。它适用于默认列,但对于我的列,如果我编辑这些文本框,则事件不会被触发!!!那么我如何实现它呢


(我有时对WPF感到惊讶!!!)

问题零如果在设置数据网格时使用
AutoGenerateColumns=“true”
,您可以在数据网格中生成列。它不会在以后动态添加列,但如果您重置itemssource,可能会这样做?(这一点不是肯定的)

问题一DataGrid具有属性
AlternatingRowBackground
AlternationCount
来设置交替行背景。但我看不到网格本身中任何交替的列背景。不过,您可以在datatemplate中执行此操作:

<DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
        <TextBlock Background="Red" Foreground="White">I'm Red</TextBlock>
    </DataTemplate>
</DataGridTemplateColumn.CellTemplate>
无论数据是否更改,只要CellEditingTemplate中的任何一个文本框失去焦点,就会调用我的
DataGrid\u CellEditEnding
事件处理程序,因此事情似乎适合我


您使用的是“内置”WPF one之外的其他数据网格吗?

问题2对我来说是非常重要的。。我必须解决这个问题,因为这是客户的要求。。请让我知道,如果你找到一些设置列颜色的方法!!!!My DataTemplate包含CellTemplate和CellEditingTemplate的ComboBox实例,其中UpdateSourceTrigger作为propertyChanged,但该事件没有引发对此的任何想法?
    <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>