Wpf 数据更新时在DataTemplate上触发
我有一个列表框项目的数据模板,类似于此页面上的数据模板。。。 我想更进一步,在项目发生变化时做一些事情来突出显示这些项目。例如,使用上面链接中的代码,我想在Widget.Quantity发生变化时设置一个触发器来执行某些操作。可能会让Quianty项目(没有其他)闪烁或其他东西。我该怎么做?我将相关代码包括在下面Wpf 数据更新时在DataTemplate上触发,wpf,binding,Wpf,Binding,我有一个列表框项目的数据模板,类似于此页面上的数据模板。。。 我想更进一步,在项目发生变化时做一些事情来突出显示这些项目。例如,使用上面链接中的代码,我想在Widget.Quantity发生变化时设置一个触发器来执行某些操作。可能会让Quianty项目(没有其他)闪烁或其他东西。我该怎么做?我将相关代码包括在下面 <Window.Resources> <Style x:Key="RoundedItem" TargetType="ListBoxItem">
<Window.Resources>
<Style x:Key="RoundedItem" TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border CornerRadius="10" BorderBrush="Black" BorderThickness="1" Margin="1">
<ContentPresenter />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<DataTemplate DataType="{x:Type local:Widget}">
<StackPanel Orientation="Horizontal">
<Label Content="{Binding Name}" />
<Label Content="{Binding Quantity}" />
</StackPanel>
</DataTemplate>
</Window.Resources>
<ListBox ItemsSource="{Binding Widgets}" ItemContainerStyle="{StaticResource RoundedItem}" HorizontalContentAlignment="Stretch" />
只需将触发器添加到DataTemplate.triggers集合中即可
<DataTemplate DataType="{x:Type local:Widget}">
<StackPanel x:Name="panel" Orientation="Horizontal">
<Label Content="{Binding Name}" />
<Label Content="{Binding Quantity}" />
</StackPanel>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding SomeProperty}" Value="True">
<Setter Property="Background" Value="Yellow" TargetName="panel" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
只需将触发器添加到DataTemplate.triggers集合中即可
<DataTemplate DataType="{x:Type local:Widget}">
<StackPanel x:Name="panel" Orientation="Horizontal">
<Label Content="{Binding Name}" />
<Label Content="{Binding Quantity}" />
</StackPanel>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding SomeProperty}" Value="True">
<Setter Property="Background" Value="Yellow" TargetName="panel" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
您可能需要在小部件类中添加一个属性来实现这一点。或者,如果小部件是一个模型,您可能希望将其包装在一个WidgetViewModel类中,并在其上具有“isflash”属性。然后将触发器设置为每当此“isflash”属性为True时触发。您可能需要在小部件类中添加一个属性来完成此操作。或者,如果小部件是一个模型,您可能希望将其包装在一个WidgetViewModel类中,并在其上具有“isflash”属性。然后将触发器设置为每当此“isflash”属性为True时激发。我已设法使其与EventTriggers一起工作。完成下面的代码。总之,事件触发器检测绑定值何时更改,然后短暂地将文本变成橙色
<UserControl.Resources>
<!-- instantiate an instance of the TimeSettingsCollection class -->
<c:TimeSettingsCollection x:Key="TimeSettingsCollection"/>
<Style x:Key="ListBoxItemStyle" TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border Name="ItemBorder" BorderBrush="Gray" BorderThickness="1" Margin="3" Padding="7" Background="Transparent">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="ItemBorder" Property="Background" Value="LightBlue" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<DataTemplate DataType="{x:Type c:TimeSettingsItem}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Margin="0,0,8,0"
Style="{StaticResource smallTitleStyle}">Pc Name:</TextBlock>
<TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Path=PcName}"
Style="{StaticResource textStyleTextBlock}"/>
<TextBlock Grid.Row="1" Grid.Column="0" Margin="0,0,8,0"
Style="{StaticResource smallTitleStyle}">Time:</TextBlock>
<TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Path=TimeSettings.DateTime, NotifyOnTargetUpdated=True}"
Style="{StaticResource textStyleTextBlock}" x:Name="timeTextBlock">
<TextBlock.Background>
<SolidColorBrush Color="Transparent"/>
</TextBlock.Background>
<TextBlock.Triggers>
<EventTrigger RoutedEvent="Binding.TargetUpdated">
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Storyboard.TargetName="timeTextBlock"
Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)"
To="Orange"
Duration="0:0:1"
AutoReverse="True"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</TextBlock.Triggers>
</TextBlock>
</Grid>
</DataTemplate>
</UserControl.Resources>
<DockPanel>
<ListBox Name="timeListBox" ItemsSource="{Binding Source={StaticResource TimeSettingsCollection}}" ItemContainerStyle="{StaticResource ListBoxItemStyle}" HorizontalContentAlignment="Stretch" IsSynchronizedWithCurrentItem="True">
</ListBox>
</DockPanel>
电脑名称:
时间:
我已经设法让它与EventTriggers一起工作。完成下面的代码。总之,事件触发器检测绑定值何时更改,然后短暂地将文本变成橙色
<UserControl.Resources>
<!-- instantiate an instance of the TimeSettingsCollection class -->
<c:TimeSettingsCollection x:Key="TimeSettingsCollection"/>
<Style x:Key="ListBoxItemStyle" TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border Name="ItemBorder" BorderBrush="Gray" BorderThickness="1" Margin="3" Padding="7" Background="Transparent">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="ItemBorder" Property="Background" Value="LightBlue" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<DataTemplate DataType="{x:Type c:TimeSettingsItem}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Margin="0,0,8,0"
Style="{StaticResource smallTitleStyle}">Pc Name:</TextBlock>
<TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Path=PcName}"
Style="{StaticResource textStyleTextBlock}"/>
<TextBlock Grid.Row="1" Grid.Column="0" Margin="0,0,8,0"
Style="{StaticResource smallTitleStyle}">Time:</TextBlock>
<TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Path=TimeSettings.DateTime, NotifyOnTargetUpdated=True}"
Style="{StaticResource textStyleTextBlock}" x:Name="timeTextBlock">
<TextBlock.Background>
<SolidColorBrush Color="Transparent"/>
</TextBlock.Background>
<TextBlock.Triggers>
<EventTrigger RoutedEvent="Binding.TargetUpdated">
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Storyboard.TargetName="timeTextBlock"
Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)"
To="Orange"
Duration="0:0:1"
AutoReverse="True"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</TextBlock.Triggers>
</TextBlock>
</Grid>
</DataTemplate>
</UserControl.Resources>
<DockPanel>
<ListBox Name="timeListBox" ItemsSource="{Binding Source={StaticResource TimeSettingsCollection}}" ItemContainerStyle="{StaticResource ListBoxItemStyle}" HorizontalContentAlignment="Stretch" IsSynchronizedWithCurrentItem="True">
</ListBox>
</DockPanel>
电脑名称:
时间:
是的,我以前也使用过类似的方法,在选择整个项目时更改listboxitem背景。我想检测项目中的某些内容何时发生变化,然后对该子项目应用某种效果。在给出的示例代码中,我想知道标签控件的值何时更改,然后使标签控件闪烁(或者无论如何执行一些可见的操作)。是的,我以前使用过类似的方法来获取listboxitem背景,以便在选中整个项目时更改。我想检测项目中的某些内容何时发生变化,然后对该子项目应用某种效果。在给出的示例代码中,我希望知道值何时更改,然后使标签控件闪烁(或者无论如何都要执行一些可见的操作)。我刚刚发现了EventTriggers,并认为这是一种方法,因为我希望在值更改时执行某些操作(不关心值是什么).EventTriggers依赖于RoutedEvents,因此它不像连接到虚拟机上的PropertyChanged或新事件那样简单。我刚刚发现了EventTriggers,并认为这是一种方法,因为我想在值更改时做一些事情(不关心值是什么).EventTriggers依赖于RoutedEvents,因此它不像连接到VM上的PropertyChanged或新事件那样简单。