设置单元格颜色时的Wpf Datagrid虚拟化问题 [英] Wpf Datagrid Virtualization Issue when setting cell colors

查看:121
本文介绍了设置单元格颜色时的Wpf Datagrid虚拟化问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用wpf工具包datagrid,并有一列装有切换按钮的列。如果选择了切换按钮,并且在鼠标悬停时使用以下样式,则可以更改背景颜色。不幸的是,如果启用了虚拟化,则当我在一个单元格中选择一个切换按钮并在网格中向下滚动时,我会发现其他背景也发生变化的单元格。我认为这是在滚动时虚拟化如何重用单元的错误。是否有建议解决此问题并仍使用虚拟化?

I'm working with the wpf toolkit datagrid and have a column that is populated with toggle buttons. Using the below style I change the background color if the toggle button is selected and also on mouse over. Unfortunately if I have virtualization enable, when I make a select of a toggle button in a cell and scroll down in the grid I will find other cells that have also had their background changed. I assume this is a bug in how virtualization is reusing the cells as I scroll. Any suggestion to get around this and still use virtualization?

                <Style TargetType="{x:Type ToggleButton}">
                <Setter Property="Control.Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ToggleButton}">
                                <TextBlock x:Name="Tb" Tag="{TemplateBinding Property=Tag}" Padding="{TemplateBinding Property=Padding}" Text="{TemplateBinding Property=Content}" >
                                </TextBlock>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter TargetName="Tb" Property="Background" Value="{StaticResource HoverRed}" />
                                </Trigger>
                                <Trigger Property="IsChecked" Value="True">
                                    <Setter TargetName="Tb" Property="Background" Value="{StaticResource SelectYellow}" />
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>


推荐答案

我已经解决了这个问题,通过绑定样式所针对的切换按钮的IsChecked属性。这样可以启用虚拟化功能,并使每个单元格中的模板化切换按钮的背景颜色与应有的状态正确同步。

I've been able to get around the issue, by binding the IsChecked property of the togglebutton that the style is targeting. This allows me to have virtualization turned on and keeps the background color for the templated toggle button in each cell properly in sync with what it should be.

                    <ToggleButton Tag="button" IsChecked="{Binding Path=Selected,Mode=TwoWay}" FocusVisualStyle="{x:Null}" Content="{Binding Path=MarkerName,Mode=OneWay}">
                </ToggleButton>

这篇关于设置单元格颜色时的Wpf Datagrid虚拟化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆