質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

Q&A

解決済

1回答

615閲覧

mainwindowにあるボタンでusercontrolのstorybordを発火したい

nananananana77

総合スコア2

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

1グッド

1クリップ

投稿2021/12/10 07:36

prismを使用しているwpfについてですが、
usercontrolにstorybordを設定してeventtriggerでroutedeventに自作の発火条件で発火しようとしています
条件としてmainwindowにボタンをいくつかつけてどのボタンが押されても同じstorybordが再生できるようにしたいです

mainwindowのボタンの参照の仕方がわからなくて困ってます

<xaml> <Canvas.Triggers> <EventTrigger RoutedEvent="MainWindow_Click"> <BeginStoryboard> ストーリーボードの内容 </BeginStoryboard> </<EventTrigger> </Canvas.Triggers>

<C#>

void MainWindow_Click(object sender, EventArgs e)
{
処理内容 :
}

よろしくお願いいたします

TN8001👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

TN8001

2021/12/10 09:02

Prism UserControlですか?(ContentRegionに突っ込む奴) それとも前回の質問のリンクのコントロールの話でしょうか? EventTriggerやBeginStoryboardに、こだわる理由がありますか?(UserControlのプロパティ変更で動くほうが使いやすそうです) もしパラパラアニメの話だとすると、ブログの内容が(おそらくAdvent Calendarに間に合わせるため急いで書いたせいで)間違っているというか不完全で、今nananananana77さんの手元でどうなっているかが予想がつかないです。
nananananana77

2021/12/10 09:45

回答ありがとうございます あちらは勉強のために質問してみたものなので前回の質問のものとは別の仕様で動かしてます。 今は単純にstorybordにDiscreteDoubleKeyFrame設定してスプライトシート作って切り替えてます。 特にこだわりはないので別回答があるならご教授いただきたいです
guest

回答1

0

ベストアンサー

先入観でIsEnabledVisibilityのように状態を切り替えるイメージを持っていたのですが、ボタンで発火するってことは期間があるようなアニメーションなんでしょうか?

プロパティって言ってしまったのでプロパティでやってみましたが、なんか違う気がします^^;

xml

1<Window 2 x:Class="Questions373203.MainWindow" 3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 5 xmlns:local="clr-namespace:Questions373203" 6 Width="800" 7 Height="450" 8 Button.Click="Window_Button_Click"> 9 10 <!-- むりやりEventTrigger使うとこうだがそこまでする?感がw --> 11 <!--<Window.Triggers> 12 <EventTrigger RoutedEvent="Button.Click"> 13 <BeginStoryboard> 14 <Storyboard> 15 <BooleanAnimationUsingKeyFrames Storyboard.TargetName="userControl1" Storyboard.TargetProperty="IsAnimating"> 16 <DiscreteBooleanKeyFrame KeyTime="0" Value="True" /> 17 </BooleanAnimationUsingKeyFrames> 18 </Storyboard> 19 </BeginStoryboard> 20 </EventTrigger> 21 </Window.Triggers>--> 22 23 <DockPanel> 24 <StackPanel DockPanel.Dock="Top" Orientation="Horizontal"> 25 <CheckBox Content="CheckBox" IsChecked="{Binding IsAnimating, ElementName=userControl1, Mode=TwoWay}" /> 26 <Button Content="Button" /> 27 <Button Content="Button" /> 28 </StackPanel> 29 <local:UserControl1 x:Name="userControl1" /> 30 </DockPanel> 31</Window>

cs

1using System.Windows; 2 3namespace Questions373203 4{ 5 public partial class MainWindow : Window 6 { 7 public MainWindow() => InitializeComponent(); 8 9 private void Window_Button_Click(object sender, RoutedEventArgs e) 10 => userControl1.IsAnimating = true; 11 } 12}

xml

1<UserControl 2 x:Class="Questions373203.UserControl1" 3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 5 x:Name="root"> 6 <UserControl.Resources> 7 <Storyboard x:Key="MyRoundingAnimation"> 8 <DoubleAnimationUsingKeyFrames Completed="DoubleAnimationUsingKeyFrames_Completed" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(RotateTransform.Angle)"> 9 <LinearDoubleKeyFrame KeyTime="00:00:00.5" Value="360" /> 10 </DoubleAnimationUsingKeyFrames> 11 </Storyboard> 12 </UserControl.Resources> 13 <Rectangle 14 Width="100" 15 Height="100" 16 RenderTransformOrigin="0.5 0.5" 17 Stroke="Red" 18 StrokeThickness="5"> 19 <Rectangle.RenderTransform> 20 <TransformGroup> 21 <RotateTransform /> 22 </TransformGroup> 23 </Rectangle.RenderTransform> 24 <Rectangle.Style> 25 <Style> 26 <Style.Triggers> 27 <DataTrigger Binding="{Binding IsAnimating, ElementName=root}" Value="True"> 28 <DataTrigger.EnterActions> 29 <BeginStoryboard Name="bsb" Storyboard="{StaticResource MyRoundingAnimation}" /> 30 </DataTrigger.EnterActions> 31 <DataTrigger.ExitActions> 32 <StopStoryboard BeginStoryboardName="bsb" /> 33 </DataTrigger.ExitActions> 34 </DataTrigger> 35 </Style.Triggers> 36 </Style> 37 </Rectangle.Style> 38 </Rectangle> 39</UserControl>

cs

1using System; 2using System.Windows; 3using System.Windows.Controls; 4 5namespace Questions373203 6{ 7 public partial class UserControl1 : UserControl 8 { 9 public bool IsAnimating { get => (bool)GetValue(IsAnimatingProperty); set => SetValue(IsAnimatingProperty, value); } 10 public static readonly DependencyProperty IsAnimatingProperty 11 = DependencyProperty.Register(nameof(IsAnimating), typeof(bool), typeof(UserControl1), 12 new FrameworkPropertyMetadata(false)); 13 14 public UserControl1() => InitializeComponent(); 15 16 private void DoubleAnimationUsingKeyFrames_Completed(object sender, EventArgs e) 17 { 18 BeginAnimation(IsAnimatingProperty, null); 19 IsAnimating = false; 20 } 21 } 22}

参考
[C#/WPF] Storyboardの動かし方(Startのさせ方) あれこれ - Qiita

方法: ストーリーボードを使用してアニメーション化した後にプロパティを設定する - WPF .NET Framework | Microsoft Docs

templates - EventTrigger with Setter in WPF? - Stack Overflow


アニメーションは難しいですね。。。

投稿2021/12/10 12:10

編集2023/07/29 12:29
TN8001

総合スコア9405

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

nananananana77

2021/12/11 00:16

ありがとうございます modeの切り替えでもできるんですねぇ いろいろ試してみたいと思います
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問