选中后,RadioButton会更改背景颜色

时间:2016-11-10 10:56:08

标签: wpf

我想覆盖ToggleButton样式,使其看起来像<RadioButton Content="Point"> <RadioButton.Template> <ControlTemplate> <ToggleButton IsChecked="{Binding IsChecked, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}" Content="{Binding Content, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"/> <ControlTemplate.Triggers> <Trigger Property="IsChecked" Value="True"> <Setter Property="Background" Value="Red"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </RadioButton.Template> </RadioButton> 一个

        $collection = Mage::getModel('catalog/product')->getCollection();

        $collection->addAttributeToSelect('image');
        $collection->addAttributeToSelect('small_image');
        $collection->addAttributeToSelect('thumbnail');
        $collection->addAttributeToSelect('product_url');
        $collection->addAttributeToSelect('id');
        $collection->addAttributeToSelect('name');
        $collection->addAttributeToSelect('priceHtml');
        $collection->addAttributeToSelect('short_description');
        $collection->addAttributeToSelect('review_summary_Html');
        $collection->addAttributeToSelect('add_to_cart_url');
        $collection->addAttributeToSelect('add_url');

我需要在选中时更改按钮的背景颜色。我尝试使用触发器,但它无法正常工作。

1 个答案:

答案 0 :(得分:2)

解决方案并不简单。问题与以下事实有关:togglebutton在检查时具有默认背景,并且必须将其删除,然后再将其更改为其他背景。

请看一下这个主题:changing background color of togglebutton when checked

以下是应该执行您想要的代码(使用此主题作为参考remove blue highlight on buttons wpf):

 <RadioButton Content="Point">
        <RadioButton.Template>
            <ControlTemplate>
                <ToggleButton IsChecked="{Binding IsChecked, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
                              Content="{Binding Content, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}" Background="Transparent">
                    <ToggleButton.Style>
                        <Style TargetType="ToggleButton">
                            <Setter Property="OverridesDefaultStyle" Value="True"/>
                            <Setter Property="Margin" Value="5"/>
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="ToggleButton">
                                        <Border Name="border" 
                                                BorderThickness="1"
                                                Padding="4,2" 
                                                BorderBrush="DarkGray" 
                                                CornerRadius="3" 
                                                Background="{TemplateBinding Background}">
                                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                                        </Border>
                                        <ControlTemplate.Triggers>
                                            <Trigger Property="IsMouseOver" Value="True">
                                                <Setter TargetName="border" Property="BorderBrush" Value="Black" />
                                            </Trigger>
                                            <Trigger Property="IsChecked" Value="true">
                                                <Setter TargetName="border" Property="Background" Value="Red"/>
                                            </Trigger>
                                        </ControlTemplate.Triggers>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </ToggleButton.Style>
                </ToggleButton>
            </ControlTemplate>
        </RadioButton.Template>
    </RadioButton>