如果值保持不变,则WPF DataTrigger不触发
我有一个WPF数据触发器,设置为在值为true时触发 我希望每次将此值设置为true时触发此触发器,即使之前为true。不幸的是,它似乎只有在值从true更改为false或反之亦然时才会触发。我的底层数据模型正在触发INotifyPropertyChanged的PropertyChanged事件,即使该值连续两次设置为true,但触发器似乎没有检测到这一点 不管绑定值是否已更改,是否仍可以使触发器运行 你们中的一些人要求提供代码,所以在这里。有趣的是,每次都会调用转换器。问题更具体到运行动画 更新 如果我更改代码以将值重置为false,然后再次重置为true,则会触发动画。显然,这并不理想,也不能使代码易于阅读。我希望有更好的方法来做到这一点 非常感谢您的帮助 WPF代码如果值保持不变,则WPF DataTrigger不触发,wpf,Wpf,我有一个WPF数据触发器,设置为在值为true时触发 我希望每次将此值设置为true时触发此触发器,即使之前为true。不幸的是,它似乎只有在值从true更改为false或反之亦然时才会触发。我的底层数据模型正在触发INotifyPropertyChanged的PropertyChanged事件,即使该值连续两次设置为true,但触发器似乎没有检测到这一点 不管绑定值是否已更改,是否仍可以使触发器运行 你们中的一些人要求提供代码,所以在这里。有趣的是,每次都会调用转换器。问题更具体到运行动画 更
<Grid>
<Grid.Resources>
<Storyboard x:Key="AnimateCellBlue">
<ColorAnimation Storyboard.TargetProperty="Background.Color" From="Transparent" To="Blue" Duration="0:0:0.1" AutoReverse="True" RepeatBehavior="1x" />
</Storyboard>
</Grid.Resources>
<TextBox Name="txtBox" Text="{Binding DataContext.DisplayText, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}">
<TextBox.Style>
<Style TargetType="TextBox">
<Style.Triggers>
<DataTrigger Binding="{Binding DataContext.IsTrue, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard Name="BidSizeUpStoryB" Storyboard="{StaticResource AnimateCellBlue}" />
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
</Grid>
数据项:-
public class DataItem : INotifyPropertyChanged
{
private bool _isTrue;
private string _displayText;
public bool IsTrue
{
get { return _isTrue; }
set
{
_isTrue = value;
NotifyPropertyChanged("IsTrue");
}
}
public string DisplayText
{
get
{
return _displayText;
}
set
{
_displayText = value;
NotifyPropertyChanged("DisplayText");
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
private void NotifyPropertyChanged(string info)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
无论设置的值如何,触发器都将触发。每当为与触发器绑定的属性引发PropertyChanged事件时,将调用触发器。这是我核实过的样品-
<TextBox>
<TextBox.Style>
<Style TargetType="TextBox">
<Style.Triggers>
<DataTrigger Binding="{Binding DataContext.IsTrue, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window},
Converter={StaticResource MyConverter}}" Value="True">
<Setter Property="Background" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
我将断点放在转换器上,每当我为property IsTrue引发PropertyChanged事件时,它都会被调用。
您的代码中还存在其他一些问题。你能告诉我你的代码在哪里遇到这个问题吗?更新:我的第一个答案似乎取决于我的ValueConverter。所以,我做了更多的研究,发现了条件行为和属性的改变 这是我测试过的一个
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
x:Class="WpfControlLibrary1.UserControl1"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded"/>
</UserControl.Triggers>
<Grid>
<i:Interaction.Triggers>
<!--Use multiple PropertyChangedTriggers to handle multiple conditions-->
<!--True State-->
<ei:PropertyChangedTrigger Binding="{Binding DataContext.IsTrue,
RelativeSource={RelativeSource AncestorType={x:Type Window}, Mode=FindAncestor}}" >
<i:Interaction.Behaviors>
<!--This is just a humble demonstration of Conditional Behavior, it's has so much potential that you can replicate complex if conditions-->
<ei:ConditionBehavior>
<ei:ConditionalExpression ForwardChaining="And">
<ei:ComparisonCondition Operator="Equal"
LeftOperand="{Binding DataContext.IsTrue, RelativeSource={RelativeSource AncestorType={x:Type Window}, Mode=FindAncestor}}"
RightOperand="True"
/>
</ei:ConditionalExpression>
</ei:ConditionBehavior>
</i:Interaction.Behaviors>
<!--I'm not sure why, but I needed to apply the default state first so that the true state plays it's storyboard.-->
<!--If you don't do this, this behaves like a data trigger.-->
<ei:GoToStateAction StateName="DefaultState"/>
<ei:GoToStateAction StateName="TrueState"/>
</ei:PropertyChangedTrigger>
<!--False state-->
<ei:PropertyChangedTrigger Binding="{Binding DataContext.IsTrue,
RelativeSource={RelativeSource AncestorType={x:Type Window}, Mode=FindAncestor}}" >
<i:Interaction.Behaviors>
<ei:ConditionBehavior>
<ei:ConditionalExpression ForwardChaining="And">
<ei:ComparisonCondition Operator="Equal"
LeftOperand="{Binding DataContext.IsTrue, RelativeSource={RelativeSource AncestorType={x:Type Window}, Mode=FindAncestor}}"
RightOperand="False"
/>
</ei:ConditionalExpression>
</ei:ConditionBehavior>
</i:Interaction.Behaviors>
<ei:GoToStateAction StateName="DefaultState"/>
</ei:PropertyChangedTrigger>
</i:Interaction.Triggers>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="VisualStateGroup">
<VisualState x:Name="DefaultState"/>
<VisualState x:Name="TrueState">
<Storyboard Storyboard.TargetName="txtBox" >
<ColorAnimation Storyboard.TargetProperty="Background.Color" From="Transparent" To="Blue" Duration="0:0:0.1" AutoReverse="True" RepeatBehavior="1x" />
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<TextBox x:Name="txtBox" Text="{Binding DataContext.DisplayText, RelativeSource={RelativeSource AncestorType={x:Type Window}, Mode=FindAncestor}}"/>
</Grid>
</UserControl>
您可以为此使用可视状态组。此外,行为属性和值都可以进行数据绑定
我在blend中很快就做到了,没有进行测试。这与我的解决方案类似
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
x:Class="WpfControlLibrary1.UserControl1"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<i:Interaction.Behaviors>
<ei:DataStateBehavior Binding="{Binding DataContext.IsTrue,
RelativeSource={RelativeSource AncestorType={x:Type Window}, Mode=FindAncestor}}"
Value="True"
TrueState="TrueState"/>
</i:Interaction.Behaviors>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="VisualStateGroup">
<VisualState x:Name="DefaultState"/>
<VisualState x:Name="TrueState">
<Storyboard Storyboard.TargetName="txtBox" >
<ColorAnimation Storyboard.TargetProperty="Background.Color" From="Transparent" To="Blue" Duration="0:0:0.1" AutoReverse="True" RepeatBehavior="1x" />
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<TextBox x:Name="txtBox" Text="{Binding DataContext.DisplayText, RelativeSource={RelativeSource AncestorType={x:Type Window}, Mode=FindAncestor}}"/>
</Grid>
</UserControl>
向我们显示属性和触发器的代码,这样我们就不必猜测了。请参阅上面的示例代码。动画将只启动一次。请查看我的问题和代码示例。是动画没有触发
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
x:Class="WpfControlLibrary1.UserControl1"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<i:Interaction.Behaviors>
<ei:DataStateBehavior Binding="{Binding DataContext.IsTrue,
RelativeSource={RelativeSource AncestorType={x:Type Window}, Mode=FindAncestor}}"
Value="True"
TrueState="TrueState"/>
</i:Interaction.Behaviors>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="VisualStateGroup">
<VisualState x:Name="DefaultState"/>
<VisualState x:Name="TrueState">
<Storyboard Storyboard.TargetName="txtBox" >
<ColorAnimation Storyboard.TargetProperty="Background.Color" From="Transparent" To="Blue" Duration="0:0:0.1" AutoReverse="True" RepeatBehavior="1x" />
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<TextBox x:Name="txtBox" Text="{Binding DataContext.DisplayText, RelativeSource={RelativeSource AncestorType={x:Type Window}, Mode=FindAncestor}}"/>
</Grid>
</UserControl>