开发者

WPF DataTrigger not firing if value is unchanged

开发者 https://www.devze.com 2023-04-12 23:02 出处:网络
I have a WPF data trigger that is set to fire when a value is true. I want this trigger to fire everytime this value is set to t开发者_运维知识库rue, even if it was true before. Unfortunately it seem

I have a WPF data trigger that is set to fire when a value is true.

I want this trigger to fire everytime this value is set to t开发者_运维知识库rue, even if it was true before. Unfortunately it seems only to fire if the value is changed from true to false or vise versa. My underlying data model is firing the PropertyChanged event of INotifyPropertyChanged even if the value is set to true twice in succession but the Trigger doesn't seem to pick this up.

Is there anyway to make the trigger run regardless of whether the bound value has changed?

Interesting to note that converters will be called each time. The problem is more specific to running an animation.

If I change my code to reset the value to false and then back to true again it does fire the animation. Obviously this is not ideal and doesn't make the code nice to read. I'm hoping there is a better way to do this.

Any help greatly appreciated.

WPF code

<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>

Code Behind:-

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;
    }
}

DataItem:-

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));
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消