Visual Studio 2017 Community (以下VS)
WPFを使用
Windows 7 Pro (32bit)
中国語のWPF Tutorial終了後、別途、UserControlについて学習中。
以下を見つけた。
http://blog.okazuki.jp/entry/2014/09/08/203943
試してみた。
エラー
XAMLファイル
NuemricUpDown.xaml
<UserControl x:Class="_170528_t1127_UserControl.NumericUpDown"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:_170528_t1127_UserControl"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="PositiveNegative">
<VisualState x:Name="Positive"/>
<VisualState x:Name="Negative"/>
<Storyboard>
<ColorAnimation
Storyboard.TargetName="textBlockValue"
Storyboard.TargetProperty="Foreground.Color"
To="Red" />
</Storyboard>
</VisualStateGroup>"
</VisualStateManager.VisualStateGroups>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<RepeatButton Content="Up" Grid.Column="1" Margin="2.5" Click="UpButton_Click"/>
<RepeatButton Content="Down" Grid.Column="1" Grid.Row="1"
Margin="2.5" Click="DownButton_Click"/>
<TextBlock x:Name="textBlockValue" Grid.RowSpan="2" TextWrapping="Wrap"
Width="40"
HorizontalAlignment="Right" VerticalAlignment="Center" Margin="5"
Foreground="Black"/>
</Grid>
</UserControl>
エラーメッセージ
上記の<Storyboard>
の部分で以下のエラーが出る。
型 'Storyboard'のインスタンスを型 'FreezableCollection `1'のコレクションに追加することはできません。型 'T'の項目のみが許可されます。
訂正
訂正部分
<VisualState x:Name="Negative"/>
を以下に変更した。
<VisualState x:Name="Negative">
( <Storyboard>
の階層がおかしな場所になっていた)。
修正版XAML
NumericUpDown.xaml
<UserControl x:Class="_170528_t1127_UserControl.NumericUpDown"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:_170528_t1127_UserControl"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="PositiveNegative">
<VisualState x:Name="Positive"/>
<VisualState x:Name="Negative">
<Storyboard>
<ColorAnimation
Storyboard.TargetName="textBlockValue"
Storyboard.TargetProperty="Foreground.Color"
To="Red" />
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<RepeatButton Content="Up" Grid.Column="1" Margin="2.5" Click="UpButton_Click"/>
<RepeatButton Content="Down" Grid.Column="1" Grid.Row="1"
Margin="2.5" Click="DownButton_Click"/>
<TextBlock x:Name="textBlockValue" Grid.RowSpan="2" TextWrapping="Wrap"
Width="40"
HorizontalAlignment="Right" VerticalAlignment="Center" Margin="5"
Foreground="Black"/>
</Grid>
</UserControl>