gpt4 book ai didi

wpf - WPF 中切换按钮周围的轮廓

转载 作者:行者123 更新时间:2023-12-04 15:54:58 24 4
gpt4 key购买 nike

我有一些切换按钮可以控制某些选项卡的可见性。按钮的样式为按下时变为蓝色,按下时变为灰色。问题是,当按下一个切换按钮然后另一个切换按钮将切换回灰色但具有蓝色轮廓。如果选择了其他控件(不是切换按钮之一),则该按钮将变为灰色而没有蓝色轮廓。我不想要蓝色轮廓。

我的风格是:

<Style x:Key="TabToggleButtonStyle" TargetType="{x:Type ToggleButton}">
<Setter Property="Background" Value="lightGray"/>
<Setter Property="Margin" Value="3" />
<Setter Property="Padding" Value="7,2,7,2" />
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Background" Value="#FF41B8F2" />
</Trigger>
</Style.Triggers>
</Style>

切换:
<DockPanel LastChildFill="False">
<ToggleButton
DockPanel.Dock="Right"
Style="{StaticResource TabToggleButtonStyle}"
Click="BtnOneClick"
IsChecked="{Binding ElementName=_mainTabControl, Path=BtnOneTab.IsSelected}"
Content="Agent Info"/>

<ToggleButton
DockPanel.Dock="Right"
Style="{StaticResource TabToggleButtonStyle}"
Click="BtnTwoClick"
IsChecked="{Binding ElementName=_mainTabControl, Path=BtnTwoTab.IsSelected}"
Content="Help"/>
</DockPanel>

单击命令集 IsSelected = true在选项卡上。

最佳答案

我认为您的问题是 ToggleButton 的模板正在使用 ButtonChrome为可能的状态设置样式,例如 IsKeyboardFocused .

因此,当您取消选中 ToggleButton 时它仍将具有键盘焦点(因为您刚刚单击了它),因此它仍将被设置为这样的样式。
ToggleButton的默认模板看起来像这样

<ControlTemplate TargetType="{x:Type ToggleButton}">
<MS_Themes:ButtonChrome x:Name="Chrome"
BorderBrush="{TemplateBinding BorderBrush}"
Background="{TemplateBinding Background}"
RenderMouseOver="{TemplateBinding IsMouseOver}"
RenderPressed="{TemplateBinding IsPressed}"
RenderDefaulted="{TemplateBinding Button.IsDefaulted}"
SnapsToDevicePixels="true">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}"
RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</MS_Themes:ButtonChrome>
<ControlTemplate.Triggers>
<Trigger Property="IsKeyboardFocused" Value="true">
<Setter Property="RenderDefaulted" TargetName="Chrome" Value="true"/>
</Trigger>
<Trigger Property="IsChecked" Value="true">
<Setter Property="RenderPressed" TargetName="Chrome" Value="true"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="#ADADAD"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>

删除 IsKeyboardFocused触发器应该可以解决您的问题(这还需要您添加对 PresentationFramework.Aero 的引用)。但是因为你不能只改变模板的一部分(这是一个全有或全无的交易),你必须重写它

所以 ToggleButton风格将是
<Style x:Key="TabToggleButtonStyle" TargetType="{x:Type ToggleButton}">
<Setter Property="Background" Value="lightGray"/>
<Setter Property="Margin" Value="3" />
<Setter Property="Padding" Value="7,2,7,2" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}"
xmlns:MS_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero">
<MS_Themes:ButtonChrome x:Name="Chrome"
BorderBrush="{TemplateBinding BorderBrush}"
Background="{TemplateBinding Background}"
RenderMouseOver="{TemplateBinding IsMouseOver}"
RenderPressed="{TemplateBinding IsPressed}"
RenderDefaulted="{TemplateBinding Button.IsDefaulted}"
SnapsToDevicePixels="true">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}"
RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</MS_Themes:ButtonChrome>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="true">
<Setter Property="RenderPressed" TargetName="Chrome" Value="true"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="#ADADAD"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Background" Value="#FF41B8F2" />
</Trigger>
</Style.Triggers>
</Style>

关于wpf - WPF 中切换按钮周围的轮廓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6837804/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com