我遇到的问题是,开始故事板时,它会按预期更改对象中的值,但是没有可见的动画。情节提要板运行时没有任何反应,然后完成时,对象将立即获得情节提要板的最终结果(使用HoldEnd
)。故事板的持续时间基本上就像一个延迟。
Scrollviewer
与Stackpanel
一起使用以显示动画的自定义用户控件。这些项目的高度为28像素,并且包含用于展开和折叠包含数据的节的按钮。 MainWindow
中收到事件并使用Scrollviewer
添加到scrData.Dispatcher.Invoke(()=>{})
。我怀疑问题可能是拥有这些控件的线程。由于它们是在Scrollviewer
的调度程序方法中创建的,是否意味着UI线程拥有它们,或者触发事件的线程拥有它们?
我以前使用基本上相同的代码来制作动画,所以唯一的真正区别是这些控件是在运行时创建的。
任何帮助将不胜感激:)
这是我的代码的相关位:
MainWindow.cs:(接收数据,生成项目):
private void DataPoller_Update(object sender, DataPollerEventArgs e)
{
//New Data arrives here
if (e.message == "results")
{
DisplayData(e.data);
}
}
public void DisplayData(List<DataItem> data){
scrData.Dispatcher.Invoke(() => {
int i = 0;
foreach (DataItem item in data) {
if (IsInCurrentList(item.id)) {
Update_ExistingItemInfo(item);
}
else {
//Add new item to StackPanel
CustomControls.data_item newItem = new CustomControls.data_item(item);
stkData.Children.Insert(i, newItem);
}
i++;
}
}
}
从UserControl的.cs:
DataItem data;
public data_item(DataItem newData)
{
InitializeComponent();
data = newData;
PopulateDataFields();
//THESE ROWS SHOULD EXPAND OR COLLAPSE ON btnExpand_Click
grdMain.RowDefinitions[1].Height = new GridLength(0);
grdMain.RowDefinitions[2].Height = new GridLength(0);
}
private void btnExpand_Click(object sender, RoutedEventArgs e)
{
//For some reason, can't find reference to sb_Expand directly.
btnExpand.Dispatcher.Invoke(()=> {
if (rowDrilldown.ActualHeight > 0) {
Storyboard sb = (Storyboard)this.Resources["sb_Collapse"];
sb.Begin();
}
else {
Storyboard sb = (Storyboard)this.Resources["sb_Expand"];
sb.Begin();
}
});
}
从UserControl的xaml:
<UserControl.Resources>
<Storyboard x:Key="sb_Expand">
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(RowDefinition.Height)" Storyboard.TargetName="rowDrilldown" BeginTime="0" FillBehavior="HoldEnd">
<DiscreteObjectKeyFrame KeyTime="0:0:0">
<DiscreteObjectKeyFrame.Value>
<GridLength>0</GridLength>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
<DiscreteObjectKeyFrame KeyTime="0:0:0.25">
<DiscreteObjectKeyFrame.Value>
<GridLength>400</GridLength>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="sb_Collapse">
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(RowDefinition.Height)" Storyboard.TargetName="rowDrilldown" BeginTime="0" FillBehavior="HoldEnd">
<DiscreteObjectKeyFrame KeyTime="0:0:0">
<DiscreteObjectKeyFrame.Value>
<GridLength>400</GridLength>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
<DiscreteObjectKeyFrame KeyTime="0:0:0.25">
<DiscreteObjectKeyFrame.Value>
<GridLength>0</GridLength>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</UserControl.Resources>
<Border BorderBrush="AliceBlue" BorderThickness="1" CornerRadius="4">
<Grid x:Name="grdMain">
<Grid.RowDefinitions>
<RowDefinition Height="28"/>
<RowDefinition Height="165"/>
<RowDefinition x:Name="rowDrilldown" Height="400"/>
</Grid.RowDefinitions>
<!--Textboxes and other CustomControls-->
</Grid>
</Border>
<Grid x:Name="grdMain">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="grdMain_VisualStateGroup">
<VisualState x:Name="ExpandIncident" Storyboard="{StaticResource sb_Expand}"/>
<VisualState x:Name="CollapseIncident" Storyboard="{StaticResource sb_Collapse}"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
//在按钮上单击:bool tmp = ExtendedVisualStateManager.GoToElementState(this.grdMain as FrameworkElement,“ ExpandIncident”,true);