当我在cannot found property Background.Opacity
上使用“动画”时,我患有Listview
。
请参阅我的Gif。下图显示了我当前的工作状态。
我收到一条错误消息,指出-找不到属性Backgorund.Opactiy
。我认为这很奇怪,因为它在我进行排序之前一直有效。
请先参阅我的xaml来源。
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="2*" />
<RowDefinition />
</Grid.RowDefinitions>
<ListView ItemsSource="{Binding bsources}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<StackPanel.Style>
<Style TargetType="{x:Type StackPanel}">
<Style.Triggers>
<DataTrigger Binding="{Binding Checked}" Value="True">
<Setter Property="Background">
<Setter.Value>
<DrawingBrush
x:Name="errorBrush"
TileMode="Tile"
Viewport="0 0 1 1"
ViewportUnits="Absolute">
<DrawingBrush.RelativeTransform>
<RotateTransform Angle="45" />
</DrawingBrush.RelativeTransform>
<DrawingBrush.Drawing>
<GeometryDrawing>
<GeometryDrawing.Geometry>
<LineGeometry StartPoint="0,0" EndPoint="4,2" />
</GeometryDrawing.Geometry>
<GeometryDrawing.Pen>
<Pen Brush="Red" Thickness="4" />
</GeometryDrawing.Pen>
</GeometryDrawing>
</DrawingBrush.Drawing>
</DrawingBrush>
</Setter.Value>
</Setter>
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<!-- Storyboard.TargetProperty = "opacity" throws no error -->
<DoubleAnimationUsingKeyFrames
FillBehavior="Stop"
Storyboard.TargetProperty="Background.Opacity"
Duration="0:0:1">
<LinearDoubleKeyFrame KeyTime="0:0:0.0" Value="0.2" />
<LinearDoubleKeyFrame KeyTime="0:0:0.3" Value="0.3" />
<LinearDoubleKeyFrame KeyTime="0:0:0.5" Value="0.8" />
<LinearDoubleKeyFrame KeyTime="0:0:1" Value="1" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>
</StackPanel.Style>
<CheckBox IsChecked="{Binding Checked}" />
<TextBlock Margin="20,0,0,0" Text="{Binding Name}" />
<TextBlock Margin="20,0,0,0" Text="{Binding Address}" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Button
Name="Sort"
Grid.Row="2"
Click="Sort_Click"
Content="Sort" />
</Grid>
列表视图具有项目来源。如果项目的属性Checked
为true,则它以storyboard
开头,并且其背景变为红色drawingbrush
。我认为此错误很奇怪的原因是,当我单击一个项目并使Checked
属性为true时,它可以很好地工作。但是当我对ObservableCollection
进行排序并分配new ObservableCollection
时,它将引发错误。 ...
在xaml代码中,请参阅我的评论。在Lines,我尝试不带Background
前缀。 Storyboard.TargetProperty="Opacity"
然后它起作用。见下图。
您可以通知第一张图片和第二张图片之间的差异,但让我向您解释差异。第一个仅更改其背景画笔的不透明度,但是第二个更改所有内容(包括文本)的不透明度。我想要实现第一个动画而没有任何错误。
感谢您阅读。
我认为csharp代码没有问题-但我也会发布我的源代码。以下来源是我的csharp来源。
public partial class MainWindow : Window, INotifyPropertyChanged
{
bool state = false;
private ObservableCollection<Person> _bSources;
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChagned([CallerMemberName] string name = null) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
public ObservableCollection<Person> bsources
{
get { return _bSources; }
set { _bSources = value; OnPropertyChagned(); }
}
public MainWindow()
{
bsources = new ObservableCollection<Person>
{ new Person("김", "부산"), new Person("정", "강원도"), new Person("최", "서울"), new Person("권", "전라도"), new Person("박", "대전") };
InitializeComponent();
DataContext = this;
}
private void Sort_Click(object sender, RoutedEventArgs e)
{
if (state)
{
bsources = new ObservableCollection<Person>(bsources.OrderBy(i => i.Checked));
state = false;
}
else
{
bsources = new ObservableCollection<Person>(bsources.OrderByDescending(i => i.Checked));
state = true;
}
}
}
public class Person : INotifyPropertyChanged
{
private bool _Checked;
public bool Checked
{
get { return _Checked; }
set { _Checked = value; OnPropertyChagned(); }
}
private string name;
public string Name
{
get { return name; }
set { name = value; OnPropertyChagned(); }
}
private string _Address;
public string Address
{
get { return _Address; }
set { _Address = value; OnPropertyChagned(); }
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChagned([CallerMemberName] string name=null) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
public Person(string name, string Address)
{
this.Checked = false;
this.Name = name;
this.Address = Address;
}
}
我相信问题是,当您分配ObservableCollection的新实例时,Stackpanel尚未呈现(它的DrawingBrush背景动画)。即使在渲染StackPanel背景之前,也会首先触发DataTrigger EnterAction。在这种假设下,我设法通过更改DataTrigger.EnterActions到DataTrigger.ExitActions。我还需要添加一个备用默认默认DrawingBrush,以防您删除支票。在更新的样式下面:
<Style TargetType="{x:Type StackPanel}">
<Style.Setters>
<Setter Property="Background">
<Setter.Value>
<DrawingBrush />
<!--A default drawing brush-->
</Setter.Value>
</Setter>
</Style.Setters>
<Style.Triggers>
<DataTrigger Binding="{Binding Checked}"
Value="True">
<Setter Property="Background">
<Setter.Value>
<DrawingBrush x:Name="errorBrush"
TileMode="Tile"
Viewport="0 0 1 1"
ViewportUnits="Absolute">
<DrawingBrush.RelativeTransform>
<RotateTransform Angle="45" />
</DrawingBrush.RelativeTransform>
<DrawingBrush.Drawing>
<GeometryDrawing>
<GeometryDrawing.Geometry>
<LineGeometry StartPoint="0,0"
EndPoint="4,2" />
</GeometryDrawing.Geometry>
<GeometryDrawing.Pen>
<Pen Brush="Red"
Thickness="4" />
</GeometryDrawing.Pen>
</GeometryDrawing>
</DrawingBrush.Drawing>
</DrawingBrush>
</Setter.Value>
</Setter>
<DataTrigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<!-- Storyboard.TargetProperty = "opacity" throws no error -->
<DoubleAnimationUsingKeyFrames FillBehavior="Stop"
Storyboard.TargetProperty="Background.Opacity"
Duration="0:0:1">
<LinearDoubleKeyFrame KeyTime="0:0:0.0"
Value="0.2" />
<LinearDoubleKeyFrame KeyTime="0:0:0.3"
Value="0.3" />
<LinearDoubleKeyFrame KeyTime="0:0:0.5"
Value="0.8" />
<LinearDoubleKeyFrame KeyTime="0:0:1"
Value="1" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</DataTrigger.ExitActions>
</DataTrigger>
</Style.Triggers>
</Style>