Wpf Datagrid Virtualization Issue when setting cell colors

孤者浪人 提交于 2019-12-23 03:23:31

问题


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>

回答1:


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>


来源:https://stackoverflow.com/questions/713912/wpf-datagrid-virtualization-issue-when-setting-cell-colors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!