2014-03-27 48 views
0

我一直试图在控件中显示一条消息,该消息仅在几秒钟内出现,当我收到一条消息,指出我已发布的更新已成功接收。WPF StoryBoard仅在第一次通话时触发

我有实现INotifyPropertyChanged如下一类:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows; 

namespace Wpf.GUI.Views 
{ 
public class AlertBarStatus : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    public event RoutedEventHandler VisibleChanged; 

    private bool _visible ; 
    private bool _success ; 

    public AlertBarStatus():this (false,false) 
    { 

    } 


    public AlertBarStatus(bool visible, bool success) 
    { 
     _visible = visible; 
     _success = success; 
    } 


    public bool Visible 
    { 
     get { return _visible; } 
     set 
     { 
      _visible = value; 
      OnPropertyChanged("Visible"); 

      if(VisibleChanged != null) 
      { 
       VisibleChanged(this, null); 
      } 
     } 
    } 

    public bool Success 
    { 
     get { return _success; } 
     set 
     { 
      _success = value; 
      OnPropertyChanged("Success"); 
     } 
    } 


    protected virtual void OnPropertyChanged(string propertyName) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 
} 

我有XAML其中包含如下控制:

<UserControl x:Class="Wpf.GUI.AlertBarControl" 
     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" 
     mc:Ignorable="d" 
     d:DesignHeight="300" d:DesignWidth="300" 
     DataContext="{Binding RelativeSource={RelativeSource Self}}" 
     > 
<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="auto"/> 
    </Grid.RowDefinitions> 
    <Border x:Name="alertStatusBorder" Height="50" Visibility="Collapsed" Opacity="0" HorizontalAlignment="Stretch" > 
     <Border.Style> 
      <Style TargetType="Border"> 
       <Style.Triggers> 
        <DataTrigger Binding="{Binding AlertBarStatus.Success}" Value="True"> 
         <Setter Property="Background" Value="{StaticResource EnabledBorderBackground}" /> 
        </DataTrigger> 
        <DataTrigger Binding="{Binding AlertBarStatus.Success}" Value="False"> 
         <Setter Property="Background" Value="{StaticResource DisabledBorderBackground}" /> 
        </DataTrigger> 

        <DataTrigger x:Name="VisibilityTrigger" Binding="{Binding AlertBarStatus.Visible}" Value="True"> 
         <DataTrigger.EnterActions> 
          <BeginStoryboard> 
           <Storyboard x:Name="VisibilityStoryboard"> 
            <ObjectAnimationUsingKeyFrames Duration="0:0:0" Storyboard.TargetProperty="(UIElement.Visibility)"> 
             <DiscreteObjectKeyFrame Value="{x:Static Visibility.Visible}"/> 
            </ObjectAnimationUsingKeyFrames> 
            <DoubleAnimation Storyboard.TargetProperty="Opacity" 
           From="0" To="100" Duration="0:0:5" /> 
            <DoubleAnimation Storyboard.TargetProperty="Opacity" 
           From="100" To="0" Duration="0:0:5" /> 
            <ObjectAnimationUsingKeyFrames Duration="0:0:5" Storyboard.TargetProperty="(UIElement.Visibility)"> 
             <DiscreteObjectKeyFrame Value="{x:Static Visibility.Collapsed}"/> 
            </ObjectAnimationUsingKeyFrames> 
           </Storyboard> 
          </BeginStoryboard> 
         </DataTrigger.EnterActions> 
         <DataTrigger.ExitActions> 
          <StopStoryboard BeginStoryboardName="VisibilityStoryboard"/> 
         </DataTrigger.ExitActions> 
        </DataTrigger> 



       </Style.Triggers> 
      </Style> 
     </Border.Style> 
     <DockPanel > 
      <TextBlock DockPanel.Dock="Left" x:Name="alertStatusText" Grid.Column="0" HorizontalAlignment="Left"> 
       <TextBlock.Style> 
        <Style TargetType="TextBlock"> 
         <Style.Triggers> 
          <DataTrigger Binding="{Binding AlertBarStatus.Success}" Value="True"> 
           <Setter Property="Text" Value="Successfully published updates" /> 
          </DataTrigger> 
          <DataTrigger Binding="{Binding AlertBarStatus.Success}" Value="False"> 
           <Setter Property="Text" Value="Failed to publish updates" /> 
          </DataTrigger> 
         </Style.Triggers> 
         <Setter Property="FontSize" Value="20"></Setter> 
         <Setter Property="VerticalAlignment" Value="Center"></Setter> 
        </Style> 
       </TextBlock.Style> 
      </TextBlock> 
      <!--<Button Height="20" Width="20" Margin="3" DockPanel.Dock="Right" HorizontalAlignment="Right" 
        Background="Transparent" BorderBrush="Black" 
        Click="CloseAlertControl_Click">X</Button>--> 
     </DockPanel> 

    </Border> 

</Grid> 

的DataTrigger命名VisibilityTrigger不正是我想要什么,但只有当成功值第一次改变时。它不会再被调用。

我知道这个问题有几种不同的答案,但我尝试了所有我能找到的解决方案,但到目前为止,他们都没有为我工作。

为了澄清,可见性属性可以设置为true多次,然后它被设置为false,因此我想这可能是问题,因为我没有将值设置为不同的值,但我可以看到被调用的OnProperty改变的方法,所以我不希望这是一个问题。

任何帮助,将不胜感激!

回答

0

最后,我放弃了试图在xaml中做到这一点,而是将对象可见性绑定到了我的基础对象的可见性属性,如下所示。

<Border x:Name="alertStatusBorder" Height="50" Visibility="{Binding Path=AlertBarStatus.Visible, Converter={StaticResource BoolToVis}}"> 

我然后使用的计时器与该能见度变量设置为假并禁用其工作就像一个魅力的计时器10秒后触发的事件。

定时器代码:

alertBarControl.AlertBarStatus.Visible = true; 
_alertBarTimer.Enabled = true; 

这可能不是做的最好方式,但它肯定不是最糟糕的是我不是:这台知名度

Timer _alertBarTimer = new Timer(10000); 
_alertBarTimer.Elapsed += _alert_timer_elapsed; 

private void _alert_timer_elapsed(object sender, ElapsedEventArgs e) 
{ 
    alertBarControl.AlertBarStatus.Visible = false; 
    _alertBarTimer.Enabled = false; 
} 

代码直接在代码中设置控件的属性,而是使用绑定到控件的对象。

如果有人有这样做的更好的方法,请随时提供:)

相关问题