2012-03-26 27 views
3

MVVMデザインパターンを使用しています。 XAMLとC#のコーディングMVVMを使用して、バインドされたプロパティが変更されたときにデータバインドされたテキストブロックをフェードアウトする方法

ユーザーが新しいレコードを保存するときに「保存されたレコード」がテキストブロックに表示され、その後消えてしまいます。

これは私が働きたいものの一種である:

<TextBlock Name="WorkflowCreated" Text="Record saved"> 
    <TextBlock.Triggers> 
    <DataTrigger Binding="{Binding Path=NewWorkflowCreated}"> 
     <DataTrigger.EnterActions> 
     <BeginStoryboard> 
      <Storyboard> 
       <DoubleAnimation 
       Storyboard.TargetName="WorkflowCreated" 
       Storyboard.TargetProperty="(TextBlock.Opacity)" 
       From="1.0" To="0.0" Duration="0:0:3"/> 
      </Storyboard> 
     </BeginStoryboard> 
    </DataTrigger.EnterActions> 
    </DataTrigger> 
</TextBlock.Triggers> 

NewWorkflowCreatedがのviewmodelに変更されたときに、それは残念ながら、これは動作しません、アニメーションをトリガします。私もこれを試してみました:

<TextBlock Name="Message" Text="This is a test."> 
<TextBlock.Triggers> 
    <EventTrigger RoutedEvent="TextBlock.Loaded"> 
    <BeginStoryboard> 
    <Storyboard> 
     <DoubleAnimation 
     Storyboard.TargetName="Message" 
     Storyboard.TargetProperty="(TextBlock.Opacity)" 
     From="1.0" To="0.0" Duration="0:0:3"/> 
    </Storyboard> 
    </BeginStoryboard> 
    </EventTrigger> 
</TextBlock.Triggers> 
</TextBlock> 

何か助けていただければ幸いです。おそらく、Viewモデルにコードが必要な場合がありますか?

答えて

4

でアニメーションをキックオフについて見ることができ」、Viewで処理する必要がありますスタイル内にある必要があるDataTriggerを使用しています。

<Window.DataContext> 
    <WpfApplication2:TestViewModel/> 
</Window.DataContext> 

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition/> 
     <RowDefinition/> 
    </Grid.RowDefinitions> 
    <Grid.Resources> 
     <Style x:Key="textBoxStyle" TargetType="{x:Type TextBlock}"> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding Path=NewWorkflowCreated}" Value="True"> 
        <DataTrigger.EnterActions> 
         <BeginStoryboard> 
          <Storyboard> 
           <DoubleAnimation 
            Storyboard.TargetProperty="(TextBlock.Opacity)" 
            From="1.0" To="0.0" Duration="0:0:3"/> 
          </Storyboard> 
         </BeginStoryboard> 
        </DataTrigger.EnterActions> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </Grid.Resources> 
    <TextBlock Name="WorkflowCreated" Style="{StaticResource textBoxStyle}" Text="Record saved" /> 
    <Button Content="press me" Grid.Row="1" Click="Button_Click_1"/> 
</Grid> 

public class TestViewModel : INotifyPropertyChanged 
{ 
    private bool _newWorkflowCreated; 
    public bool NewWorkflowCreated 
    { 
     get { return _newWorkflowCreated; } 
     set { 
      _newWorkflowCreated = value; 
      PropertyChanged(this, new PropertyChangedEventArgs("NewWorkflowCreated")); 
     } 
    } 


    #region Implementation of INotifyPropertyChanged 

    public event PropertyChangedEventHandler PropertyChanged; 

    #endregion 
} 
+0

私はあなたに永遠に借りている。残念ながら、私はあなたにまだこれについての投票を与えることはできませんが、多分いつか... –

+0

あなたはおそらく答えを受け入れることができますか? – Phil

0

UI固有の動作のこの種のは間違いないViewModel

私はTextChangedイベントに見て示唆し、そこ

+0

TextBlock' 'には' TextChanged'イベントはありません。バインディング宣言の 'NotifyOnTargetUpdated'が次善策です。 – maxp

関連する問題