2017-12-14 29 views
2

私が行うためにラベルの内容をアニメーション化しようとしています表示されていません。コードの下C#WPFアニメーションラベルのコンテンツは、

<Label Grid.Row="2" x:Name="CurrentTask" 
     FontFamily="Microsoft Sans Serif" FontSize="18" FontStyle="Italic" FontWeight="Bold" 
     Foreground="White" Content="Loading"> 
    <Label.Template> 
     <ControlTemplate> 
      <ControlTemplate.Triggers> 
       <Trigger Property="IsEnabled" Value="False"> 
        <Trigger.EnterActions> 
         <BeginStoryboard> 
          <Storyboard> 
           <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PART_Content" Storyboard.TargetProperty="Content" Duration="00:00:00.8" RepeatBehavior="Forever"> 
            <DiscreteObjectKeyFrame KeyTime="00:00:00.0" Value="Loading"/> 
            <DiscreteObjectKeyFrame KeyTime="00:00:00.2" Value="Loading."/> 
            <DiscreteObjectKeyFrame KeyTime="00:00:00.4" Value="Loading.."/> 
            <DiscreteObjectKeyFrame KeyTime="00:00:00.6" Value="Loading..."/> 
           </ObjectAnimationUsingKeyFrames> 
          </Storyboard> 
         </BeginStoryboard> 
        </Trigger.EnterActions> 
       </Trigger> 
      </ControlTemplate.Triggers> 
     </ControlTemplate> 
    </Label.Template> 
</Label> 

問題は、ラベルの内容は、WPFのウィンドウに表示されていないということです。それは表示されていません。私は間違って何をしていますか?タイプのラインでも

<DiscreteObjectKeyFrame KeyTime="00:00:00.0" Value="Loading"/> 

の代わりにハードコーディング私はドットにラベル内容を連結したい値を、私はこれを行うことができますか?それぞれDiscreteObjectKeyFrameに「読み込み中」の文字列を繰り返す必要はありません。

UPDATE:代わりでIsEnabledの昇給トリガの = Trueの場合、私はラベルスタイル適用した場合の例については、以下のようlabel.loaded使用しています:トリガーのほか

<Label Grid.Row="2" x:Name="CurrentTask" 
      FontFamily="Microsoft Sans Serif" FontSize="18" FontStyle="Italic" 
      Foreground="White" Content="Loading"> 
     <Label.Style> 
      <Style TargetType="Label"> 
       <Style.Triggers> 
        <EventTrigger RoutedEvent="Label.Loaded"> 
         <EventTrigger.Actions> 
          <BeginStoryboard> 
           <Storyboard> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Content" Duration="00:00:00.8" RepeatBehavior="Forever"> 
             <DiscreteObjectKeyFrame KeyTime="00:00:00.0" Value="Loading"/> 
             <DiscreteObjectKeyFrame KeyTime="00:00:00.2" Value="Loading."/> 
             <DiscreteObjectKeyFrame KeyTime="00:00:00.4" Value="Loading.."/> 
             <DiscreteObjectKeyFrame KeyTime="00:00:00.6" Value="Loading..."/> 
            </ObjectAnimationUsingKeyFrames> 
           </Storyboard> 
          </BeginStoryboard> 
         </EventTrigger.Actions> 
        </EventTrigger> 
       </Style.Triggers> 
      </Style> 
     </Label.Style> 
    </Label> 

答えて

2

を、あなたのControlTemplateであります空であるので、明らかに何も表示されません。

ContentPresenterを追加して、Storyboard.TargetNameも削除できます。あなたのTriggerがIsEnabledに設定されていて、falseに設定されていると奇妙に思えます。コンテンツで繰り返される「ロード」の文字列の場合

<Label ...> 
    <Label.Style> 
     <Style TargetType="Label"> 
      <Style.Triggers> 
       <Trigger Property="IsEnabled" Value="True"> 
        <Trigger.EnterActions> 
         <BeginStoryboard> 
          <Storyboard> 
           <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Content" Duration="00:00:00.8" RepeatBehavior="Forever"> 
            <DiscreteObjectKeyFrame KeyTime="00:00:00.0" Value="Loading"/> 
            <DiscreteObjectKeyFrame KeyTime="00:00:00.2" Value="Loading."/> 
            <DiscreteObjectKeyFrame KeyTime="00:00:00.4" Value="Loading.."/> 
            <DiscreteObjectKeyFrame KeyTime="00:00:00.6" Value="Loading..."/> 
           </ObjectAnimationUsingKeyFrames> 
          </Storyboard> 
         </BeginStoryboard> 
        </Trigger.EnterActions> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
    </Label.Style> 
</Label> 

、ちょうど2つを使用します。

<Label.Template> 
    <ControlTemplate TargetType="Label"> 

     <ContentPresenter/> 

     <ControlTemplate.Triggers> 
      <Trigger Property="IsEnabled" Value="True"> 
       <Trigger.EnterActions> 
        <BeginStoryboard> 
         <Storyboard> 
          <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Content" Duration="00:00:00.8" RepeatBehavior="Forever"> 
           <DiscreteObjectKeyFrame KeyTime="00:00:00.0" Value="Loading"/> 
           <DiscreteObjectKeyFrame KeyTime="00:00:00.2" Value="Loading."/> 
           <DiscreteObjectKeyFrame KeyTime="00:00:00.4" Value="Loading.."/> 
           <DiscreteObjectKeyFrame KeyTime="00:00:00.6" Value="Loading..."/> 
          </ObjectAnimationUsingKeyFrames> 
         </Storyboard> 
        </BeginStoryboard> 
       </Trigger.EnterActions> 
      </Trigger> 
     </ControlTemplate.Triggers> 
    </ControlTemplate> 
</Label.Template> 

しかし、あなたが実際に持っているしたい場合がありますことは代わりのControlTemplateトリガーのスタイルトリガがありますラベルは固定された「読み込み中」の文字列、もう1つはドット付きで、パディングを調整します。または、2つのTextBlock:

<StackPanel Orientation="Horizontal"> 
    <TextBlock Text="Loading"/> 
    <TextBlock> 
     <TextBlock.Style> 
      <Style TargetType="TextBlock"> 
       <Style.Triggers> 
        <Trigger Property="IsEnabled" Value="True"> 
         <Trigger.EnterActions> 
          <BeginStoryboard> 
           <Storyboard> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Text" Duration="00:00:00.8" RepeatBehavior="Forever"> 
             <DiscreteObjectKeyFrame KeyTime="00:00:00.0" Value=""/> 
             <DiscreteObjectKeyFrame KeyTime="00:00:00.2" Value="."/> 
             <DiscreteObjectKeyFrame KeyTime="00:00:00.4" Value=".."/> 
             <DiscreteObjectKeyFrame KeyTime="00:00:00.6" Value="..."/> 
            </ObjectAnimationUsingKeyFrames> 
           </Storyboard> 
          </BeginStoryboard> 
         </Trigger.EnterActions> 
        </Trigger> 
       </Style.Triggers> 
      </Style> 
     </TextBlock.Style> 
    </TextBlock> 
</StackPanel> 
+0

ハイClemens!迅速なテストを行うために、私はあなたのソリューションの第2を試しました。ラベルスタイルのトリガを設定しましたが、動作しません。ラベルの内容は常に「読み込み中」のままです。私はウィンドウとこのラベルを持つMVVMアプリケーションを持っており、このウィンドウはスプラッシュ画面です。このスプラッシュ画面は、ビューコンストラクタから(インスタンス化コンストラクタによって)呼び出されます。最後にShow()を呼び出すことで表示されます。これらすべてのものは、アパートの状態がSTAに設定されたスレッドを使用しています。 – user1624552

+0

...その間、ビューコンストラクタでは、フローが続行され、viewModelでdatacontextを設定し、ビューでデータベース要求が実行されますモデルコンストラクタ。これが完了すると、スプラッシュ画面が閉じます。たぶん、それは動作していないスレッドのためですか? – user1624552

+0

私が言ったように、あなたのトリガーがIsEnabledに作用してfalseに設定されていることは奇妙に思えます。それは意図されていますか? – Clemens

関連する問題