2012-10-01 3 views
2

바운드 값이 바뀔 때 배경색을 변경하는 레이블을 얻으려고합니다. 작동이되면 일시적으로 녹색으로 깜박입니다. 꺼지면 일시적으로 빨간색으로 깜박입니다. 나는 응용 프로그램을 실행하면레이블 값이 변경 될 때 배경색을 애니메이션하는 방법

나는 이러한 문제를 얻을 :

  • 레이블 색깔 시대의 작은 소수 애니메이션을 한 후 바로 중지하고 때로는 다시 레이블을
  • 을 변경하지 않는 대부분의 시간 단지 적색 또는 녹색으로 이동하고에 붙어 유지에 관계없이 입력의 입찰이 떨어지면, 확산은 아직, 같은 색상에 애니메이션 3 개 라벨을 증가
  • 값 (있는 경우 애니메이션 작품)

누구나이 문제에 대해 잘 알고 있고 이것을 구조화하는 더 좋은 방법에 대해 의견을 개진 할 수 있습니까? 만약 값이 위 또는 아래로 갔는지 확인하는 더 좋은 방법은 입찰가에 대한 ViewModel에서 6 가지 속성을 필요로하지 않고 묻고 전파하는 것인가 궁금합니다. 또한 매우 자주 값을 변경하는 경우 (초당 5+라고 말하면됩니까?) 궁금합니다.

감사합니다.


보기

<Window x:Class="TestApp.UI.View.QuoteView" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="QuoteView" Height="300" Width="300"> 
    <Window.Resources> 
     <Style x:Key="BidStyle"> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding Path=BidHigher}" Value="True"> 
        <DataTrigger.EnterActions> 
         <BeginStoryboard> 
          <Storyboard> 
           <ColorAnimation Storyboard.TargetProperty="Background.Color" To="Green" Duration="0:0:0.2" AutoReverse="True"/> 
          </Storyboard> 
         </BeginStoryboard> 
        </DataTrigger.EnterActions> 
       </DataTrigger> 
       <DataTrigger Binding="{Binding Path=BidLower}" Value="True"> 
        <DataTrigger.EnterActions> 
         <BeginStoryboard> 
          <Storyboard> 
           <ColorAnimation Storyboard.TargetProperty="Background.Color" To="Red" Duration="0:0:0.2" AutoReverse="True"/> 
          </Storyboard> 
         </BeginStoryboard> 
        </DataTrigger.EnterActions> 
       </DataTrigger> 
       <DataTrigger Binding="{Binding Path=AskHigher}" Value="True"> 
        <DataTrigger.EnterActions> 
         <BeginStoryboard> 
          <Storyboard> 
           <ColorAnimation Storyboard.TargetProperty="Background.Color" To="Green" Duration="0:0:0.2" AutoReverse="True"/> 
          </Storyboard> 
         </BeginStoryboard> 
        </DataTrigger.EnterActions> 
       </DataTrigger> 
       <DataTrigger Binding="{Binding Path=AskLower}" Value="True"> 
        <DataTrigger.EnterActions> 
         <BeginStoryboard> 
          <Storyboard> 
           <ColorAnimation Storyboard.TargetProperty="Background.Color" To="Red" Duration="0:0:0.2" AutoReverse="True"/> 
          </Storyboard> 
         </BeginStoryboard> 
        </DataTrigger.EnterActions> 
       </DataTrigger> 
       <DataTrigger Binding="{Binding Path=SpreadHigher}" Value="True"> 
        <DataTrigger.EnterActions> 
         <BeginStoryboard> 
          <Storyboard> 
           <ColorAnimation Storyboard.TargetProperty="Background.Color" To="Green" Duration="0:0:0.2" AutoReverse="True"/> 
          </Storyboard> 
         </BeginStoryboard> 
        </DataTrigger.EnterActions> 
       </DataTrigger> 
       <DataTrigger Binding="{Binding Path=SpreadLower}" Value="True"> 
        <DataTrigger.EnterActions> 
         <BeginStoryboard> 
          <Storyboard> 
           <ColorAnimation Storyboard.TargetProperty="Background.Color" To="Red" Duration="0:0:0.2" AutoReverse="True"/> 
          </Storyboard> 
         </BeginStoryboard> 
        </DataTrigger.EnterActions> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </Window.Resources> 
    <Grid> 
     <Label Content="Bid" HorizontalAlignment="Left" Margin="21,10,0,0" VerticalAlignment="Top" /> 
     <Label Content="Ask" HorizontalAlignment="Left" Margin="19,53,0,0" VerticalAlignment="Top"/> 
     <Label Content="Spread" HorizontalAlignment="Left" Margin="19,99,0,0" VerticalAlignment="Top"/> 
     <Label x:Name="BidLabel" HorizontalAlignment="Left" Margin="102,10,0,0" VerticalAlignment="Top" Content="{Binding Path=Quote.Bid}" Style="{StaticResource ResourceKey=BidStyle}"/> 
     <Label x:Name="AskLabel" HorizontalAlignment="Left" Margin="102,53,0,0" VerticalAlignment="Top" Content="{Binding Path=Quote.Ask}" Style="{StaticResource ResourceKey=BidStyle}"/> 
     <Label x:Name="SpreadLabel" HorizontalAlignment="Left" Margin="102,99,0,0" VerticalAlignment="Top" Content="{Binding Path=Quote.BidAskSpread}" Style="{StaticResource ResourceKey=BidStyle}"/> 
    </Grid> 
</Window> 

뷰 모델

public class QuoteViewModel : ViewModelBase 
{ 
    private readonly FakeDataGenerator _dataGenerator; 
    private Quote _quote; 
    private bool _bidHigher; 
    private bool _bidLower; 
    private bool _askHigher; 
    private bool _askLower; 
    private bool _spreadHigher; 
    private bool _spreadLower; 

    public QuoteViewModel() 
    { 
     _dataGenerator = new FakeDataGenerator(); 
     _dataGenerator.NewQuoteEvent += DataGeneratorOnNewQuoteEvent; 
    } 

    private void DataGeneratorOnNewQuoteEvent(Quote quote) 
    { 
     Quote = quote; 
    } 

    public Quote Quote 
    { 
     get { return _quote; } 
     set 
     { 
      if (_quote != value) 
      { 
       UpdateQuoteComparisons(_quote, value); 
       _quote = value; 
       OnPropertyChanged("Quote"); 
      } 
     } 
    } 

    private void UpdateQuoteComparisons(Quote existingQuote, Quote newQuote) 
    { 
     if(existingQuote == null) 
     { 
      return; 
     } 

     if (newQuote.Bid > existingQuote.Bid) 
     { 
      BidHigher = true; 
     } 
     else if (newQuote.Bid < existingQuote.Bid) 
     { 
      BidLower = true; 
     } 

     if (newQuote.Ask > existingQuote.Ask) 
     { 
      AskHigher = true; 
     } 
     else if (newQuote.Ask < existingQuote.Ask) 
     { 
      AskLower = true; 
     } 

     if (newQuote.BidAskSpread > existingQuote.BidAskSpread) 
     { 
      SpreadHigher = true; 
     } 
     else if (newQuote.BidAskSpread < existingQuote.BidAskSpread) 
     { 
      SpreadLower = true; 
     } 
    } 

    public bool BidHigher 
    { 
     get { return _bidHigher; } 
     set 
     { 
      _bidHigher = value; 
      OnPropertyChanged("BidHigher"); 
     } 
    } 

    public bool BidLower 
    { 
     get { return _bidLower; } 
     set 
     { 
      _bidLower = value; 
      OnPropertyChanged("BidLower"); 
     } 
    } 

    public bool AskHigher 
    { 
     get { return _askHigher; } 
     set 
     { 
      _askHigher = value; 
      OnPropertyChanged("AskHigher"); 
     } 
    } 

    public bool AskLower 
    { 
     get { return _askLower; } 
     set 
     { 
      _askLower = value; 
      OnPropertyChanged("AskLower"); 
     } 
    } 

    public bool SpreadHigher 
    { 
     get { return _spreadHigher; } 
     set 
     { 
      _spreadHigher = value; 
      OnPropertyChanged("SpreadHigher"); 
     } 
    } 

    public bool SpreadLower 
    { 
     get { return _spreadLower; } 
     set 
     { 
      _spreadLower = value; 
      OnPropertyChanged("SpreadLower"); 
     } 
    } 
} 

답변

2

당신은 (당신이 BeginStoryboard의 기존 이름을해야합니다)를 DataTrigger.ExitActions를 통해 스토리 보드를 중지 시도 할 수 :

 <DataTrigger Binding="{Binding Path=SpreadLower}" Value="True"> 
      <DataTrigger.EnterActions> 
       <BeginStoryboard Name="SpreadLowerStoryboard> 
        <Storyboard> 
         <ColorAnimation Storyboard.TargetProperty="Background.Color" To="Red" Duration="0:0:0.2" AutoReverse="True"/> 
        </Storyboard> 
       </BeginStoryboard> 
      </DataTrigger.EnterActions> 
      <DataTrigger.ExitActions> 
       <StopStoryboard BeginStoryboardName="SpreadLowerStoryboard" /> 
      </DataTrigger.ExitActions> 
     </DataTrigger> 
+0

나는 가셔서 감사 드리겠습니다. – Michael

관련 문제