wpf - 无论新值如何,属性更改时都会触发数据触发器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6198077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
wpf - fire datatrigger when property changes regardless of new value
提问by Peanut
I'm trying to execute an animation on a cell in a datagrid when the value of the datagrid cell changes.
当 datagrid 单元格的值发生变化时,我试图在 datagrid 中的单元格上执行动画。
The datagrid itself is bound to an ObservableCollection of plain old CLR objects. In this case lets say the objects are 'Person' objects with properties for 'Firstname', 'Lastname' and 'Age'. The 'Person' class implements the INotifyPropertyChanged interface and each property has the appropriate call to onPropertyChanged in it's setter.
数据网格本身绑定到普通旧 CLR 对象的 ObservableCollection。在这种情况下,假设对象是具有“名字”、“姓氏”和“年龄”属性的“人”对象。'Person' 类实现了 INotifyPropertyChanged 接口,并且每个属性在它的 setter 中都有对 onPropertyChanged 的适当调用。
This is all fine. In the datagrid definition I've set my DataTemplate for drawing each cell and attached a datatrigger too ... as follows:
这一切都很好。在数据网格定义中,我设置了用于绘制每个单元格的 DataTemplate 并附加了一个数据触发器......如下:
<DataGridTemplateColumn Header="FirstName">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Border Name="templateBorder">
<TextBlock Name="templateTextBlock" Text="{Binding Path=FirstName}" />
</Border>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=FirstName}" Value="Richard">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard AutoReverse="True">
<DoubleAnimation Storyboard.TargetName="templateTextBlock" Storyboard.TargetProperty="Opacity" To=".1" Duration="0:0:.5" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
When an object in my ObservableCollection is updated (I changed the FirstName value) the datagrid is updated fine. As per the example above, if I changed the value of FirstName to 'Richard' then the animation is executed fine too.
当我的 ObservableCollection 中的对象更新(我更改了 FirstName 值)时,数据网格会更新得很好。根据上面的示例,如果我将 FirstName 的值更改为 'Richard',那么动画也可以正常执行。
My problem is that I need to run my animation regardless of what the new value of Firstname is. I've crawled the web but some far only seem to find examples of firing the trigger against a known value e.g. fire trigger when FirstName is 'Richard' as I've demonstrated in my example.
我的问题是,无论 Firstname 的新值是什么,我都需要运行我的动画。我已经爬过网络,但有些人似乎只找到了针对已知值触发触发器的示例,例如,如我在示例中所演示的,当 FirstName 为“Richard”时触发触发器。
My question is how do I fire the datatrigger regardless of the value of the updated property? So basically how do I fire the datatrigger whenever the FirstName property is updated for a given row in the datagrid.
我的问题是,无论更新属性的值如何,如何触发数据触发器?所以基本上,每当为数据网格中的给定行更新 FirstName 属性时,我如何触发数据触发器。
Many thanks.
非常感谢。
回答by Peanut
Thanks to the pointers gained from the responses to this question I found the answer was to use an EventTrigger and the TargetUpdated RoutedEvent.
感谢从对这个问题的回答中获得的指针,我发现答案是使用 EventTrigger 和 TargetUpdated RoutedEvent。
<DataTemplate>
<Border Name="templateBorder">
<TextBlock Name="templateTextBlock" Text="{Binding Path=FirstName, NotifyOnTargetUpdated=True}" />
</Border>
<DataTemplate.Triggers>
<EventTrigger RoutedEvent="Binding.TargetUpdated">
<BeginStoryboard>
<Storyboard AutoReverse="True">
<DoubleAnimation Storyboard.TargetName="templateTextBlock" Storyboard.TargetProperty="Opacity" To=".1" Duration="0:0:.5" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</DataTemplate.Triggers>
</DataTemplate>
Beyond the EventTrigger, the only other thing that was required was to set 'NotifyOnTargetUpdated=True' when setting up the binding for the textblock.
除了 EventTrigger 之外,唯一需要做的就是在为文本块设置绑定时设置“NotifyOnTargetUpdated=True”。
Thanks.
谢谢。
回答by Gishu
It looks like you need an EventTrigger"do X when an event occurs" instead of a DataTrigger.
看起来您需要一个EventTrigger“在事件发生时执行 X”而不是 DataTrigger。
Not tried this myself.. but it should be possible to raise your custom event FirstNameChanged and have the trigger-actions be executed in response to that.
我自己没有尝试过这个..但是应该可以引发您的自定义事件 FirstNameChanged 并执行触发器操作以响应该事件。
回答by Bathineni
<Storyboard x:Key="MessageStoryBoardEntry" FillBehavior="Stop">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)">
<EasingDoubleKeyFrame KeyTime="0:0:00.30" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:03" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:03.20" Value="1500"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="MessageStoryBoardExit" FillBehavior="Stop">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)">
<EasingDoubleKeyFrame KeyTime="0:0:0.001" Value="1500"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
回答by Ray
You could try setting the DataContext of the TextBlock to the FirstName property, and then use the DataContextChangedevent.
您可以尝试将 TextBlock 的 DataContext 设置为 FirstName 属性,然后使用DataContextChanged事件。
Or you could use the PropertyChangedevent and filter for the property you want.
或者您可以使用PropertyChanged事件并筛选您想要的属性。
Either way I think you're going to have to use an event.
无论哪种方式,我都认为您将不得不使用事件。
回答by MarcE
Could you hack something in with a value converter?
你能用价值转换器来破解吗?
<DataTrigger Binding="{Binding Path=FirstName, Converter=FirstNameConverter}" Value="MakeItSo">
and
和
class FirstNameConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return "MakeItSo";
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
...
}
}
I guess it depends on whether WPF calls the converter on every property change, or whether it evaluates the value first. I've not tried it, it's just a thought...
我想这取决于 WPF 是在每次属性更改时调用转换器,还是首先评估值。我没试过,只是一个想法……