我将ItemsControl与Canvas作为ItemsPanel。
<ItemsControl ItemsSource="{Binding Path=Characters, ElementName=Playground}" >
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Canvas.Left" Value="5" x:Name="PlaygroundCanvas" />
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<v:CharacterControl
PlayerName="{Binding PlayerName}"
CharacterImage="{Binding CharacterImage}"
FrameWidth="{Binding FrameWidth}"
FrameHeight="{Binding FrameHeight}"
Location="{Binding Location}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
在我的CharacterControl中可以通过编程方式更改Canvas.Left属性吗?
实际上我想将动画绑定到我想出的这个样式属性,但它并不适用于所有CharacterControl.xaml.cs:
_movementAnimation = (Storyboard)this.FindResource("Movement");
Storyboard.SetTarget(_movementAnimation, this);
在CharacterControl.xaml中指定的动画如下:
<UserControl.Resources>
<Storyboard x:Key="Movement" Storyboard.TargetProperty="(Canvas.Left)" Completed="Storyboard_Completed">
<DoubleAnimation Duration="0:0:0.750" />
</Storyboard>
</UserControl.Resources>