gpt4 book ai didi

WPF DataBound ListBox 添加动画但不滚动

转载 作者:行者123 更新时间:2023-12-02 00:08:15 31 4
gpt4 key购买 nike

我有一个绑定(bind)到 ObservableCollection 的 WPF ListBox。当我向其中添加项目时,我想要一些动画来“吸引注意力”到“新到货”。有许多使用 DataTemplate 触发器和 FrameworkElment.Loaded 事件执行此操作的示例。效果很好,看起来很酷。

但是,滚动列表时也会触发相同的动画。这是由于 ListBox 的虚拟化功能,它会根据需要“加载”新项目。

有没有一种方法可以构造一个 ListBox,使其仅在 ObservableCollection 发生变化时触发“已加载”动画,而在滚动时不触发?

附言禁用虚拟化不是一种选择

最佳答案

为了触发动画,我创建了附加的依赖属性 AddNewItem( bool 值):

public class MyDependencyClass : DependencyObject
{
public static readonly DependencyProperty AddNewItemProperty;

public static void SetAddNewItem(DependencyObject DepObject, bool value)
{
DepObject.SetValue(AddNewItemProperty, value);
}

public static bool GetAddNewItem(DependencyObject DepObject)
{
return (bool)DepObject.GetValue(AddNewItemProperty);
}

static MyDependencyClass()
{
PropertyMetadata MyPropertyMetadata = new PropertyMetadata(false);

AddNewItemProperty = DependencyProperty.RegisterAttached("AddNewItem",
typeof(bool),
typeof(MyDependencyClass),
MyPropertyMetadata);
}
}

然后创建我们的依赖属性的边框(警告添加新项目):

<Border x:Name="WarningBorder" Style="{StaticResource HideBorderStyle}" local:MyDependencyClass.AddNewItem="False" Height="33" Background="#44515B" BorderThickness="0" Margin="65,0,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Opacity="0">
<TextBlock x:Name="WarningText" FontFamily="./#Segoe UI" TextAlignment="Center" Margin="0,5,0,0" FontSize="18" Foreground="Gainsboro" Text="You add new item" />
</Border>

样式 HideBorderStyle 包含当属性 AddNewItem=True 时触发的 DataTrigger。 HideBorderStyle 列表:

    <Style x:Key="HideBorderStyle" TargetType="{x:Type Border}">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=WarningBorder, Path=(local:MyDependencyClass.AddNewItem), Mode=OneWay}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Width" BeginTime="0:0:0" From="0.0" To="427.0" Duration="0:0:0.5" />
<DoubleAnimation Storyboard.TargetProperty="Opacity" BeginTime="0:0:0" From="0.0" To="1.0" Duration="0:0:1.0" />
<DoubleAnimation Storyboard.TargetProperty="Opacity" BeginTime="0:0:5" From="1.0" To="0.0" Duration="0:0:1.0" />
<DoubleAnimation Storyboard.TargetProperty="Width" BeginTime="0:0:5" From="427.0" To="0.0" Duration="0:0:1.0" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>

假设我们有一个 SampleListBox 和 AddButton:

 <Grid>
<ListBox x:Name="SampleListBox" Width="400" Height="200" BorderThickness="1" BorderBrush="Black" DisplayMemberPath="Name" Background="AliceBlue" Loaded="SampleListBox_Loaded" />

<Button Content="Add" Width="50" Height="30" VerticalAlignment="Bottom" Click="AddButton_Click" />
</Grid>

AddButton_Click 列表:

    private void AddButton_Click(object sender, RoutedEventArgs e)
{
PersonListBox.Add(new Person()
{
Name = "NewItem",
});

SampleListBox.ItemsSource = PersonListBox;
}

在 SampleListBox_Loaded 中,我初始化 SampleListBox 的数据并分配一个处理程序 NotifyCollectionChangedEventHandler,它在集合更改时调用。 SampleListBox_Loaded 事件列表:

private void SampleListBox_Loaded(object sender, RoutedEventArgs e)
{
PersonListBox.Add(new Person()
{
Name = "Peter Orange",
Age = 32,
Sample = "Sample",
});

SampleListBox.ItemsSource = PersonListBox;
PersonListBox.CollectionChanged += new NotifyCollectionChangedEventHandler(PersonListBox_CollectionChanged);
}

在 PersonListBox_CollectionChanged 中,我为依赖属性 AddNewItem 设置值 True。 PersonListBox_CollectionChanged list :

    private void PersonListBox_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add)
{
MyDependencyClass.SetAddNewItem(WarningBorder, true);
}
}

现在,当一个项目被添加到集合中时,动画就会被触发。如果添加另一个元素动画不会触发。所以她工作了,你需要为以后的显示动画折叠值:

private void ResetButton_Click(object sender, RoutedEventArgs e)
{
MyDependencyClass.SetAddNewItem(WarningBorder, false);
}

在这种情况下,您需要决定何时重置动画值。

编辑:

要为添加的项目设置动画,我已经基于我之前的示例。但是现在 DataTrigger 的样式将分配给 ListBoxItem:

  <ListBox x:Name="SampleListBox" local:MyDependencyClass.AddNewItem="False" local:MyDependencyClass.InitCountOfList="0" Width="400" Height="200" BorderThickness="1" BorderBrush="Black" DisplayMemberPath="Name" Background="AliceBlue" Loaded="SampleListBox_Loaded">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding ElementName=SampleListBox, Path=(local:MyDependencyClass.AddNewItem), Mode=OneWay}" Value="True" />
<Condition Binding="{Binding ElementName=SampleListBox, Converter={StaticResource BoolToInitItemsConverter}}" Value="True" />
</MultiDataTrigger.Conditions>

<MultiDataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Opacity" BeginTime="0:0:0" From="0.0" To="1.0" Duration="0:0:1.0" />
</Storyboard>
</BeginStoryboard>
</MultiDataTrigger.EnterActions>
</MultiDataTrigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>

为了正确工作,动画(尤其是在程序开始时)是根据另一个属性创建的 - InitCountOfList (Int):

public class MyDependencyClass : DependencyObject
{
public static readonly DependencyProperty AddNewItemProperty, InitCountOfListProperty;

#region AddNewItem

public static void SetAddNewItem(DependencyObject DepObject, bool value)
{
DepObject.SetValue(AddNewItemProperty, value);
}

public static bool GetAddNewItem(DependencyObject DepObject)
{
return (bool)DepObject.GetValue(AddNewItemProperty);
}

#endregion

#region InitCountOfList

public static void SetInitCountOfList(DependencyObject DepObject, int value)
{
DepObject.SetValue(InitCountOfListProperty, value);
}

public static int GetInitCountOfList(DependencyObject DepObject)
{
return (int)DepObject.GetValue(InitCountOfListProperty);
}

#endregion

#region Constructor of DependencyProperty

static MyDependencyClass()
{
PropertyMetadata BoolPropertyMetadata = new PropertyMetadata(false);
PropertyMetadata IntPropertyMetadata = new PropertyMetadata(0);

AddNewItemProperty = DependencyProperty.RegisterAttached("AddNewItem",
typeof(bool),
typeof(MyDependencyClass),
BoolPropertyMetadata);

InitCountOfListProperty = DependencyProperty.RegisterAttached("InitCountOfList",
typeof(int),
typeof(MyDependencyClass),
IntPropertyMetadata);
}

#endregion
}

数据在 Window_ContentRendered 事件中初始化:

 private void Window_ContentRendered(object sender, EventArgs e)
{
PersonListBox.Add(new Person()
{
Name = "Peter Orange",
});

SampleListBox.ItemsSource = PersonListBox;

// Set the initial number of collection
MyDependencyClass.SetInitCountOfList(SampleListBox, SampleListBox.Items.Count);
}

处理程序 NotifyCollectionChangedEventHandler 在 SampleListBox_Loaded 事件中分配:

private void SampleListBox_Loaded(object sender, RoutedEventArgs e)
{
PersonListBox.CollectionChanged += new NotifyCollectionChangedEventHandler(PersonListBox_CollectionChanged);
}

Handler 和 AddButton_Click 保持不变:

private void PersonListBox_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add)
{
MyDependencyClass.SetAddNewItem(SampleListBox, true);
}
}

private void AddButton_Click(object sender, RoutedEventArgs e)
{
PersonListBox.Add(new Person()
{
Name = "NewItem",
});
}

Converter BoolToInitItemsConverter,如果在开始期间没有添加元素,则返回 true:

/// <summary>
/// Return true, if count of collection > initial
/// </summary>
public class BoolToInitItemsConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
ListBox MyListBox = value as ListBox;
int InitCountOfList = MyDependencyClass.GetInitCountOfList(MyListBox);

if (MyListBox.Items.Count > InitCountOfList)
{
// Added a new element
return true;
}

// First run and we do not want
// to run the animation
return false;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return DependencyProperty.UnsetValue as object;
}
}

现在动画工作只针对添加的项目。

关于WPF DataBound ListBox 添加动画但不滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17010396/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com