views:

788

answers:

1

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>
A: 

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>
David Osborn