我在我的ListView ItemTemplate
上添加ColorAnimation
到VisualStateManager
时遇到问题。 VisualStateManager似乎没有改变它的视觉状态。UWP - ListView.ItemTemplate中的StateTrigger
我想在这里做的是开始一个StoryBoard
将开始尽快顺利改变对ListViewItem
的Rectangle.Fill
颜色,作为其底层视图模型的的isReady属性值的变化。
我在做什么错?以及如何正确地做到这一点(最好没有无意义的UserControl)?
这里的XAML:
<Page
x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ListView ItemsSource="{x:Bind MyList}">
<ListView.ItemTemplate>
<DataTemplate x:DataType="local:B">
<UserControl>
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="group">
<VisualState x:Name="state1">
<VisualState.StateTriggers>
<StateTrigger IsActive="{x:Bind IsReady, Mode=OneWay}"/>
</VisualState.StateTriggers>
<Storyboard>
<ColorAnimation Duration="0:0:1.8" To="Red" Storyboard.TargetProperty="(Rectangle.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="rect" />
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Rectangle x:Name="rect" Fill="Blue" Width="20" Height="20" />
</Grid>
</UserControl>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Button Click="Button_Click">Test</Button>
</Grid>
</Page>
这里是后面的代码:
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace App1
{
public abstract class NotifyPropertyChangedBase : INotifyPropertyChanged
{
protected NotifyPropertyChangedBase()
{
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void RaisePropertyChanged([CallerMemberName]string propertyName = null)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class B : NotifyPropertyChangedBase
{
private bool isReady;
public bool IsReady
{
get { return isReady; }
set { if (isReady != value) { isReady = value; RaisePropertyChanged(); } }
}
}
public sealed partial class MainPage : Page
{
public ObservableCollection<B> MyList { get; private set; } = new ObservableCollection<B>();
public MainPage()
{
this.InitializeComponent();
for (int i = 0; i < 10; i++)
{
MyList.Add(new B());
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
MyList[2].IsReady = !MyList[2].IsReady;
}
}
}
你的问题是什么?我用你的代码进行了测试,它在我身边效果很好。 –
@ JayZuo-MSFT,没有使用'UserControl',我在MainPage.g.cs的Set_Windows_UI_Xaml_StateTrigger_IsActive中收到'NullReferenceException' – Vitaly