【问题标题】:C# WPF ItemsControl item moving animationC# WPF ItemsControl 项目移动动画
【发布时间】:2016-09-18 03:51:40
【问题描述】:


我对 ItemsControl 中的动画有一点疑问
我有这个 XAML 代码

<ItemsControl ItemsSource="{Binding CellItems}" Background="Black">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
        <ItemsControl.ItemContainerStyle>
            <Style TargetType="ContentPresenter">

                <Setter Property="Canvas.Left" >
                    <Setter.Value>
                        <MultiBinding Converter="{StaticResource MultiplierConverter}">
                            <Binding Path="J"></Binding>
                            <Binding Path="DataContext.ItemSize" ElementName="ThisControl"></Binding>
                        </MultiBinding>
                    </Setter.Value>
                </Setter>
                <Setter Property="Canvas.Top" >
                    <Setter.Value>
                        <MultiBinding Converter="{StaticResource MultiplierConverter}">
                            <Binding Path="I"></Binding>
                            <Binding Path="DataContext.ItemSize" ElementName="ThisControl"></Binding>
                        </MultiBinding>
                    </Setter.Value>
                </Setter>

            </Style>
        </ItemsControl.ItemContainerStyle>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Border Width="{Binding ElementName=ThisControl, Path=DataContext.ItemSize}"
                    Height="{Binding ElementName=ThisControl, Path=DataContext.ItemSize}"
                    Style="{StaticResource CellItemBorderStyle}">
                <Border.InputBindings>
                    <MouseBinding MouseAction="LeftClick"
                                  Command="{Binding DataContext.MoveCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}}"
                                  CommandParameter="{Binding}"/>
                </Border.InputBindings>
                <TextBlock Text="{Binding Value}"
                           HorizontalAlignment="Center"
                           VerticalAlignment="Center"
                           FontSize="{Binding ElementName=ThisControl, Path=DataContext.ItemSize, Converter={StaticResource SingleMultiplierConverter}, ConverterParameter=0.4}"
                           Style="{StaticResource CellItemTextBlockStyle}"/>
            </Border>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

CellItems 是我的 ViewModel 中的一个属性

    public List<CellItem> CellItems { get; set; }

CellItem 是我的班级:

public class CellItem : ViewModelBase
{
    public int Value { get; set; }

    private int i;
    public int I
    {
        get { return this.i; }
        set
        {
            this.i = value;
            this.OnPropertyChanged(nameof(this.I));
        }
    }

    private int j;
    public int J
    {
        get { return this.j; }
        set
        {
            this.j = value;
            this.OnPropertyChanged(nameof(this.J));
        }
    }

}

I 和 J 属性在 ItemsControl.ItemContainerStyle 中绑定,您可以看到
当我更改 I(或 J)属性时如何做动画?

请随时向我询问更多详细信息
谢谢!

更新
当我单击项目时,它们正在移动但没有动画。
我需要用动画实现移动,但我不知道如何... DoubleAnimation,Storyboard?..

【问题讨论】:

  • 不知道怎么办?您具体遇到了什么问题?何时或如何触发动画?如何实现动画本身?请简化您的代码,以便您有一个良好的minimal reproducible example,清楚地显示您尝试过的内容,并准确描述该代码的作用以及您希望它做什么。

标签: c# wpf


【解决方案1】:

在这里要清楚一点,您是在尝试在属性更改时启动动画吗?实现 INotifyPropertyChanged 并引发一个事件,该事件将在您的 ViewModel 中启动动画。

【讨论】:

  • 感谢您的回答。不确定我是否理解你。能给我举个例子吗?
猜你喜欢
  • 1970-01-01
  • 2013-06-03
  • 2011-01-15
  • 1970-01-01
  • 2019-02-19
  • 2016-02-18
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
相关资源
最近更新 更多