1

我有一个切换按钮,单击它时我只想更改背景和内容。内容正确更改,但背景更改,但不是我指定的背景值,而是默认(?)蓝色。在以前的斗争中,我可以改变背景,但不能改变内容。但是,使用数据触发器而不是下面的方法以不同的方式完成。

我正在使用 C# WPF 和 MVVM 并且可以将内容绑定到另一个属性,并且当绑定的 ischecked 属性更改时,我可以更改视图模型中的属性,但我希望不必这样做。也许这是正确的方法?

这是我的 1/2 工作的代码:

        <ToggleButton Name="Axis3AbsIncButton"               
            IsChecked="{Binding Path=UserControlOneStatic.MotionParameters.Axis3AbsorIncOption, Source={StaticResource Locator}}">
                <ToggleButton.Style>
                    <Style TargetType="{x:Type ToggleButton}">
                        <Setter Property="ToggleButton.Background" Value="Goldenrod"/>
                        <Setter Property="ToggleButton.Content"
                                Value="ABS" />
                        <Style.Triggers>
                            <Trigger Property="IsChecked" Value="True">
                                <Setter Property="ToggleButton.Background"
                                        Value="Green" />
                                <Setter Property="ToggleButton.Content"
                                        Value="Inc" />
                            </Trigger>
                        </Style.Triggers>
                        </Style >
                </ToggleButton.Style>
            </ToggleButton>

这非常接近,但我失去了切换按钮的边框:

            <ToggleButton Name="Axis3AbsIncButton"
                          Grid.Row="2"
                          Grid.Column="13"
                          Grid.ColumnSpan="2"
                          Grid.RowSpan="1"
                          Focusable="True"
                          IsChecked="{Binding Path=UserControlOneStatic.MotionParameters.Axis3AbsorIncOption, Source={StaticResource Locator}}">
                <ToggleButton.Template>
                    <ControlTemplate TargetType="{x:Type ToggleButton}">
                        <Border CornerRadius="3" BorderThickness="1"  Background="{TemplateBinding Background}">
                            <ContentPresenter Margin="3"
                                              HorizontalAlignment="Center"
                                              VerticalAlignment="Center" />
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsChecked"
                                     Value="False">
                                <Setter Property="Background" 
                                        Value="Goldenrod">
                                </Setter>
                            </Trigger>                                    
                            <Trigger Property="IsChecked"
                                     Value="True">
                                <Setter Property="Background"
                                        Value="Blue" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </ToggleButton.Template>
                <ToggleButton.Style>
                    <Style TargetType="{x:Type ToggleButton}">
                        <Setter Property="ToggleButton.Content"
                                Value="ABS" />
                        <Style.Triggers>
                            <Trigger Property="IsChecked"
                                     Value="True">
                                <Setter Property="ToggleButton.Content"
                                        Value="Inc" />
                            </Trigger>
                        </Style.Triggers>
                    </Style >
                </ToggleButton.Style>
            </ToggleButton>
4

1 回答 1

1

您的 XAML 代码是正确的。当您单击 ToggleButton 时,背景颜色变为绿色。但是当您从按钮上移动鼠标时它会发生变化。当鼠标光标在 ToggleButton 上时,它具有蓝色。

编辑: 试试这个:

<ToggleButton Name="Axis3AbsIncButton"
                  Grid.Row="2"
                  Grid.Column="13"
                  Grid.ColumnSpan="2"
                  Grid.RowSpan="1"
                  Focusable="True"
                  IsChecked="{Binding Path=UserControlOneStatic.MotionParameters.Axis3AbsorIncOption, Source={StaticResource Locator}}">
        <ToggleButton.Template>
            <ControlTemplate TargetType="{x:Type ButtonBase}">
                <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                    <ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="Button.IsDefaulted" Value="True">
                        <Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="Background" TargetName="border" Value="#FFF4F4F4"/>
                        <Setter Property="BorderBrush" TargetName="border" Value="#FFADB2B5"/>
                        <Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="#FF838383"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </ToggleButton.Template>
        <ToggleButton.Style>
            <Style TargetType="{x:Type ToggleButton}">
                <Setter Property="ToggleButton.Background" Value="Goldenrod"/>
                <Setter Property="ToggleButton.Content"
                            Value="ABS" />
                <Style.Triggers>
                    <Trigger Property="IsChecked" Value="True">
                        <Setter Property="ToggleButton.Background"
                                    Value="Green" />
                        <Setter Property="ToggleButton.Content"
                                    Value="Inc" />
                    </Trigger>
                </Style.Triggers>
            </Style >
        </ToggleButton.Style>
    </ToggleButton>
于 2013-05-19T07:22:33.393 回答