1
我做了ToggleButton的擴展。它繼承自ToggleButton,並且我添加了一些依賴屬性。我希望鼠標懸停在標籤和文本上,當IsChecked爲真時。WPF切換按鈕狀態復位爲正常,鼠標懸停後未檢查狀態
Desiered結果
- 正常狀態:黑暗文本
- 鼠標懸停:淺色文字
- 經過:淺色文字
它時處於正常狀態,並在鼠標懸停時工作正常。當我切換按鈕時,它仍然突出顯示,當我從中刪除鼠標。但是當我再次懸停時,文本會變回黑暗,因爲它沒有被檢查。我究竟做錯了什麼? (如果我刪除鼠標懸停狀態,文字仍然突出顯示時)是否因爲鼠標懸停和檢查是在不同的visualstategroups?
這是我在generic.xaml
<Style TargetType="{x:Type view:ExtendedToggleButton}">
<Setter Property="BorderThickness" Value="1" />
<Setter Property="BorderBrush" Value="{DynamicResource LineBrush}" />
<Setter Property="Padding" Value="5" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type view:ExtendedToggleButton}">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver">
<Storyboard>
<!--<ColorAnimation Duration="0" Storyboard.TargetName="Background" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" To="{DynamicResource BackgroundMouseOverColor}"/>-->
<ColorAnimation Duration="0" Storyboard.TargetName="LabelControl" Storyboard.TargetProperty="(Label.Foreground).(SolidColorBrush.Color)" To="{DynamicResource TitleColor}"/>
<ColorAnimation Duration="0" Storyboard.TargetName="TextControl" Storyboard.TargetProperty="(Label.Foreground).(SolidColorBrush.Color)" To="{DynamicResource DetailTextColor}"/>
</Storyboard>
</VisualState>
<VisualState x:Name="Pressed">
<Storyboard>
<ColorAnimation Duration="0" Storyboard.TargetName="Background" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" To="{DynamicResource BackgroundMousePressedColor}"/>
</Storyboard>
</VisualState>
<VisualState x:Name="Disabled">
<Storyboard>
<DoubleAnimation Duration="0" Storyboard.TargetName="DisabledVisualElement" Storyboard.TargetProperty="Opacity" To=".55"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="CheckStates">
<VisualState x:Name="Checked">
<Storyboard>
<ColorAnimation Duration="0" Storyboard.TargetName="LabelControl" Storyboard.TargetProperty="(Label.Foreground).(SolidColorBrush.Color)" To="{DynamicResource TitleColor}"/>
<ColorAnimation Duration="0" Storyboard.TargetName="TextControl" Storyboard.TargetProperty="(Label.Foreground).(SolidColorBrush.Color)" To="{DynamicResource DetailTextColor}"/>
</Storyboard>
</VisualState>
<VisualState x:Name="Unchecked"/>
</VisualStateGroup>
<VisualStateGroup x:Name="FocusStates">
<VisualState x:Name="Focused" />
<VisualState x:Name="Unfocused" />
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border x:Name="Background" Background="Transparent" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}">
<Grid Background="{TemplateBinding Background}" Margin="1">
</Grid>
</Border>
<StackPanel>
<WrapPanel Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}">
<TextBlock x:Name="LabelControl" Margin="0 0 10 0" FontSize="14" Foreground="{DynamicResource DescreteTitleBrush}" Text="{TemplateBinding Label}" />
<TextBlock x:Name="TextControl" Margin="5 2 0 0" FontSize="12" Foreground="{DynamicResource DarkDetailTextBrush}" TextWrapping="Wrap" Text="{TemplateBinding Text}" />
</WrapPanel>
<ContentPresenter
x:Name="contentPresenter"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"/>
</StackPanel>
<Rectangle x:Name="DisabledVisualElement" Fill="#FFFFFFFF" Opacity="0" IsHitTestVisible="false" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我已經嘗試添加觸發器的控件模板代碼,但它並不能幫助。
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter TargetName="LabelControl" Property="Foreground" Value="{DynamicResource TitleBrush}" />
</Trigger>
</ControlTemplate.Triggers>