我有一个WPF数据触发器,当值为true时设置为触发.
我希望每次将此值设置为true时触发此触发器,即使之前为真.不幸的是,如果值从true变为false或反之亦然,似乎只会触发.我的基础数据模型正在触发INotifyPropertyChanged的PropertyChanged事件,即使该值连续两次设置为true,但触发器似乎没有选择它.
无论如何,无论绑定值是否已更改,都会使触发器运行?
你们中的一些人已经要求代码,所以在这里.有趣的是,每次都会调用转换器.问题更具体到运行动画.
更新 如果我更改我的代码以将值重置为false,然后再次返回到true,则会触发动画.显然这并不理想,并且不会使代码很好阅读.我希望有更好的方法来做到这一点.
任何帮助非常感谢.
WPF代码
<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>
Run Code Online (Sandbox Code Playgroud)
代码背后: -
public partial class MainWindow : Window
{
private DataItem _dataItem;
private DispatcherTimer _dispatcherTimer;
public MainWindow()
{
InitializeComponent();
_dataItem = new DataItem();
_dataItem.DisplayText = "Testing";
_dataItem.IsTrue = true;
this.DataContext = _dataItem;
_dispatcherTimer = new DispatcherTimer(TimeSpan.FromSeconds(1), DispatcherPriority.Normal, TimerCallbackHandler, Dispatcher);
}
private void TimerCallbackHandler(object s, EventArgs args)
{
Console.WriteLine("In Timer");
_dataItem.IsTrue = true;
_dataItem.DisplayText = "Timer " + DateTime.Now.Ticks;
}
}
Run Code Online (Sandbox Code Playgroud)
数据项:-
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));
}
}
Run Code Online (Sandbox Code Playgroud)
无论设置的值如何,触发器都会触发。每当为与触发器绑定的属性引发 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>
Run Code Online (Sandbox Code Playgroud)
我在转换器上放置了断点,每当我为属性 IsTrue 引发 PropertyChanged 事件时,它就会被调用。您的代码中一定存在其他问题。您能否展示一下您遇到此问题的代码?
归档时间: |
|
查看次数: |
2312 次 |
最近记录: |