不能用VisualStateManger添加按钮样式.VisualStateGroup到资源字典
本文关键字:VisualStateGroup 资源 字典 样式 按钮 VisualStateManger 添加 不能 | 更新日期: 2023-09-27 17:50:55
我有使用VisualStateManger的按钮样式。目前这些样式在<Grid.Resources>
和工作没有任何错误。我试图将这些样式移动到资源字典,它给出了以下错误。有人知道为什么style是在用户控制中工作,而当移动到资源字典时不工作吗?
标签'visualstatemanager. 'visualstategroups'在XML中不存在命名空间http://schemas.microsoft.com/winfx/2006/xaml/presentation
我使用的是。net 3.5
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vsm="clr-namespace:System.Windows;assembly=wpftoolkit"
xmlns:system="clr-namespace:System;assembly=mscorlib"
>
<Style x:Key="Home" BasedOn="{StaticResource PagingButton}" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType='Button'>
<Border Name='border' Background='{StaticResource HomeButtonBackground}' CornerRadius='5,5,0,0'>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" >
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.Background)" Storyboard.TargetName="border">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ButtonBackground}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Pressed">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.Background)" Storyboard.TargetName="border">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ButtonBackgroundPressed}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
问题是您正在尝试使用未引用的程序集中的某些内容。您需要将其添加到根目录下的Window/Page标签中,其他命名空间如
xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows"
那么你可以用
<vsm:VisualStateManager.VisualStateGroups>
<vsm:VisualStateGroup x:Name="CommonStates">
<vsm:VisualState x:Name="MouseOver">
<Storyboard>
<ColorAnimation Storyboard.TargetName="tickBox"
Storyboard.TargetProperty="(Rectangle.Fill).
(SolidColorBrush.Color)"
To="PaleGreen" Duration="0:0:0.5" />
</Storyboard>
</vsm:VisualState>
</vsm:VisualStateGroup>
</vsm:VisualStateManager.VisualStateGroups>