以编程方式在ItemTemplate中设置ItemContainerStyle属性

时间:2016-09-14 10:51:23

标签: c# wpf xaml itemscontrol

我将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>

0 个答案:

没有答案