使用VisualStateGroup公共状态名称对我的ControlTemplate中的UI没有任何影响。
在其他样式的示例中,相同的状态名称都能很好地工作,正如我在github上的Microsoft源代码中看到的那样,所以我认为这是因为我使用了一个自定义的目标控件。
TargetType="local:VolumetricButton“
有可能修复这段代码吗?
如果不是,如何在CommonStates中正确地添加和处理“ControlTemplate”
Generic.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Components">
<Style TargetType="local:VolumetricButton">
<Setter Property="Template" Value="{StaticResource VolumetricButtonTemplate}" />
</Style>
<ControlTemplate x:Key="VolumetricButtonTemplate" TargetType="local:VolumetricButton">
<Grid x:Name="RootGrid">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualStateGroup.Transitions>
<VisualTransition To="PointerOver"
GeneratedDuration="0:0:0.5"/>
</VisualStateGroup.Transitions>
<VisualState x:Name="Normal" />
<VisualState x:Name="PointerOver">
<Storyboard>
<ColorAnimation Storyboard.TargetName="BackBrush"
Storyboard.TargetProperty="Color" To="Red" />
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Rectangle x:Name="BackgroundRectangle" RadiusX="2" RadiusY="2">
<Rectangle.Fill>
<SolidColorBrush x:Name="BackBrush" Color="Green"/>
</Rectangle.Fill>
</Rectangle>
</Grid>
</ControlTemplate>
</ResourceDictionary>VolumetricButton.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Documents;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Shapes;
// The Templated Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234235
namespace Components
{
[TemplatePart(Name = BackgroundRectangleName, Type = typeof(Rectangle))]
[TemplatePart(Name = RootGridName, Type = typeof(Grid))]
public sealed class VolumetricButton : Control
{
private const string BackgroundRectangleName = "BackgroundRectangle";
private const string RootGridName = "RootGrid";
private Rectangle BackgroundRectangle;
private Grid RootGrid;
public VolumetricButton()
{
this.DefaultStyleKey = typeof(VolumetricButton);
//
// Set core events
//
this.SizeChanged += OnThisSizeChanged;
}
/// <inheritdoc/>
protected override void OnApplyTemplate()
{
base.OnApplyTemplate();
//
// New code here
//
// Find and set RootGrid
RootGrid = GetTemplateChild(RootGridName) as Grid;
// Find and set BackgroundRectangle
BackgroundRectangle = GetTemplateChild(BackgroundRectangleName) as Rectangle;
}
private void OnThisSizeChanged(object sender, SizeChangedEventArgs e)
{
if (BackgroundRectangle != null
&& RootGrid != null)
{
BackgroundRectangle.Width = RootGrid.ActualWidth;
BackgroundRectangle.Height = RootGrid.ActualHeight;
}
}
}
}
Best regards,
Roman发布于 2020-01-14 11:00:34
您目前只定义PointerOver的状态,但是没有定义如何触发状态。
试试这个:
OnApplyTemplate中
protected override void OnApplyTemplate()
{
base.OnApplyTemplate();
// ...
RootGrid.PointerEntered += RootGrid_PointerEntered;
RootGrid.PointerExited += RootGrid_PointerExited;
}事件处理方法中的
private void RootGrid_PointerExited(object sender, PointerRoutedEventArgs e)
{
VisualStateManager.GoToState(this, "Normal", true);
}
private void RootGrid_PointerEntered(object sender, PointerRoutedEventArgs e)
{
VisualStateManager.GoToState(this, "PointerOver", true);
}诚挚的问候。
https://stackoverflow.com/questions/59730628
复制相似问题