2014-03-27 77 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; 
} 

代碼直接在代碼中設置控件的屬性,而是使用綁定到控件的對象。

如果有人有這樣做的更好的方法,請隨時提供:)

相關問題