teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

見直しキャンペーン中

2023/07/23 08:38

投稿

TN8001
TN8001

スコア10111

answer CHANGED
@@ -1,121 +1,121 @@
1
- `RenderTransform`が`One`にしか付いていないので、ほかの目が出たときにエラーになります。
2
- 全部に付ければ想定通りに動きます。
3
-
4
- ```xaml
5
- <Window
6
- x:Class="Questions300589.MainWindow"
7
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
8
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
9
- Width="300"
10
- Height="450">
11
- <Window.Resources>
12
- <Storyboard x:Key="Jump">
13
- <DoubleAnimationUsingKeyFrames Storyboard.TargetName="{Binding DEME_NAME}" Storyboard.TargetProperty="(UIElement.RenderTransform).(TranslateTransform.Y)">
14
- <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="-20" />
15
- <EasingDoubleKeyFrame KeyTime="0:0:1" Value="0" />
16
- </DoubleAnimationUsingKeyFrames>
17
- </Storyboard>
18
-
19
- <Style TargetType="{x:Type TextBlock}">
20
- <Setter Property="HorizontalAlignment" Value="Center" />
21
- <Setter Property="VerticalAlignment" Value="Center" />
22
- <Setter Property="FontSize" Value="36" />
23
- <Setter Property="RenderTransform">
24
- <Setter.Value>
25
- <TranslateTransform />
26
- </Setter.Value>
27
- </Setter>
28
- </Style>
29
- </Window.Resources>
30
- <Grid>
31
- <Grid.RowDefinitions>
32
- <RowDefinition />
33
- <RowDefinition />
34
- <RowDefinition Height="3*" />
35
- </Grid.RowDefinitions>
36
- <Button
37
- HorizontalAlignment="Center"
38
- VerticalAlignment="Center"
39
- Click="Button_Click"
40
- Content="さいころを振る" />
41
-
42
- <TextBlock FontSize="24" Text="{Binding DEME}" Grid.Row="1" />
43
-
44
- <UniformGrid Grid.Row="2" Columns="2">
45
- <TextBlock x:Name="One" Text="1" />
46
- <TextBlock x:Name="Two" Text="2" />
47
- <TextBlock x:Name="Three" Text="3" />
48
- <TextBlock x:Name="Four" Text="4" />
49
- <TextBlock x:Name="Five" Text="5" />
50
- <TextBlock x:Name="Six" Text="6" />
51
- </UniformGrid>
52
- </Grid>
53
- </Window>
54
- ```
55
-
56
- ```C#
57
- using System;
58
- using System.ComponentModel;
59
- using System.Windows;
60
- using System.Windows.Media.Animation;
61
-
62
- namespace Questions300589
63
- {
64
- public partial class MainWindow : Window
65
- {
66
- private readonly Hoge hoge = new Hoge();
67
- private readonly Storyboard sb;
68
-
69
- public MainWindow()
70
- {
71
- InitializeComponent();
72
- DataContext = hoge;
73
- sb = FindResource("Jump") as Storyboard;
74
- }
75
-
76
- private void Button_Click(object sender, RoutedEventArgs e)
77
- {
78
- hoge.Shake();
79
- BeginStoryboard(sb);
80
- }
81
- }
82
-
83
- public class Hoge : INotifyPropertyChanged
84
- {
85
- private static readonly Random rnd = new Random();
86
-
87
- public int DEME { get; private set; }
88
-
89
- public string DEME_NAME
90
- {
91
- get
92
- {
93
- switch(DEME)
94
- {
95
- default:
96
- case 1: return "One";
97
- case 2: return "Two";
98
- case 3: return "Three";
99
- case 4: return "Four";
100
- case 5: return "Five";
101
- case 6: return "Six";
102
- }
103
- }
104
- }
105
-
106
- public void Shake()
107
- {
108
- DEME = rnd.Next(1, 7);
109
- RaiseProeprtyChanged(nameof(DEME));
110
- RaiseProeprtyChanged(nameof(DEME_NAME));
111
- }
112
-
113
- #region INotifyPropertyChanged の実装
114
- public event PropertyChangedEventHandler PropertyChanged;
115
- private void RaiseProeprtyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
116
- #endregion
117
- }
118
- }
119
- ```
120
-
1
+ `RenderTransform`が`One`にしか付いていないので、ほかの目が出たときにエラーになります。
2
+ 全部に付ければ想定通りに動きます。
3
+
4
+ ```xml
5
+ <Window
6
+ x:Class="Questions300589.MainWindow"
7
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
8
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
9
+ Width="300"
10
+ Height="450">
11
+ <Window.Resources>
12
+ <Storyboard x:Key="Jump">
13
+ <DoubleAnimationUsingKeyFrames Storyboard.TargetName="{Binding DEME_NAME}" Storyboard.TargetProperty="(UIElement.RenderTransform).(TranslateTransform.Y)">
14
+ <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="-20" />
15
+ <EasingDoubleKeyFrame KeyTime="0:0:1" Value="0" />
16
+ </DoubleAnimationUsingKeyFrames>
17
+ </Storyboard>
18
+
19
+ <Style TargetType="{x:Type TextBlock}">
20
+ <Setter Property="HorizontalAlignment" Value="Center" />
21
+ <Setter Property="VerticalAlignment" Value="Center" />
22
+ <Setter Property="FontSize" Value="36" />
23
+ <Setter Property="RenderTransform">
24
+ <Setter.Value>
25
+ <TranslateTransform />
26
+ </Setter.Value>
27
+ </Setter>
28
+ </Style>
29
+ </Window.Resources>
30
+ <Grid>
31
+ <Grid.RowDefinitions>
32
+ <RowDefinition />
33
+ <RowDefinition />
34
+ <RowDefinition Height="3*" />
35
+ </Grid.RowDefinitions>
36
+ <Button
37
+ HorizontalAlignment="Center"
38
+ VerticalAlignment="Center"
39
+ Click="Button_Click"
40
+ Content="さいころを振る" />
41
+
42
+ <TextBlock FontSize="24" Text="{Binding DEME}" Grid.Row="1" />
43
+
44
+ <UniformGrid Grid.Row="2" Columns="2">
45
+ <TextBlock x:Name="One" Text="1" />
46
+ <TextBlock x:Name="Two" Text="2" />
47
+ <TextBlock x:Name="Three" Text="3" />
48
+ <TextBlock x:Name="Four" Text="4" />
49
+ <TextBlock x:Name="Five" Text="5" />
50
+ <TextBlock x:Name="Six" Text="6" />
51
+ </UniformGrid>
52
+ </Grid>
53
+ </Window>
54
+ ```
55
+
56
+ ```cs
57
+ using System;
58
+ using System.ComponentModel;
59
+ using System.Windows;
60
+ using System.Windows.Media.Animation;
61
+
62
+ namespace Questions300589
63
+ {
64
+ public partial class MainWindow : Window
65
+ {
66
+ private readonly Hoge hoge = new Hoge();
67
+ private readonly Storyboard sb;
68
+
69
+ public MainWindow()
70
+ {
71
+ InitializeComponent();
72
+ DataContext = hoge;
73
+ sb = FindResource("Jump") as Storyboard;
74
+ }
75
+
76
+ private void Button_Click(object sender, RoutedEventArgs e)
77
+ {
78
+ hoge.Shake();
79
+ BeginStoryboard(sb);
80
+ }
81
+ }
82
+
83
+ public class Hoge : INotifyPropertyChanged
84
+ {
85
+ private static readonly Random rnd = new Random();
86
+
87
+ public int DEME { get; private set; }
88
+
89
+ public string DEME_NAME
90
+ {
91
+ get
92
+ {
93
+ switch(DEME)
94
+ {
95
+ default:
96
+ case 1: return "One";
97
+ case 2: return "Two";
98
+ case 3: return "Three";
99
+ case 4: return "Four";
100
+ case 5: return "Five";
101
+ case 6: return "Six";
102
+ }
103
+ }
104
+ }
105
+
106
+ public void Shake()
107
+ {
108
+ DEME = rnd.Next(1, 7);
109
+ RaiseProeprtyChanged(nameof(DEME));
110
+ RaiseProeprtyChanged(nameof(DEME_NAME));
111
+ }
112
+
113
+ #region INotifyPropertyChanged の実装
114
+ public event PropertyChangedEventHandler PropertyChanged;
115
+ private void RaiseProeprtyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
116
+ #endregion
117
+ }
118
+ }
119
+ ```
120
+
121
121
  xamlが長くなりそうだったのでだいぶ変えてしまいましたが、元の形でもOKです。