button
スタイルを独自のリソース辞書内に記述しようとしています。 基本アニメーションが必要です。マウスの上を移動すると、ボタンの背景が濃いグレーから薄いグレーにフェードします。WPF:ストーリーボードアニメーションで定義済みの色をリソース辞書内のボタンスタイルに使用
問題は、私は特にストーリーボード内のキーによって定義済みの色を参照していたという事実を好きにいないようだということです。なぜ私は既存のリソースを参照するのに慣れているのか分かりません。
例外が実行時に発生します
以下"InvalidOperationException: Cannot freeze this Storyboard timeline tree for use across threads"
は、ボタンのための未完成のスタイルです:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Style.Components.Buttons">
<Style TargetType="{x:Type Button}" x:Key="LargeFlatListOptionButtonStyle">
<Setter Property="Foreground" Value="{DynamicResource TextParagraphWhiteP1}" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Background" Value="{DynamicResource BackgroundGreyLevel2}" />
<Setter Property="MinHeight" Value="32" />
<Setter Property="MinWidth" Value="50" />
<Setter Property="Padding" Value="6,2" />
<Setter Property="BorderBrush" Value="{DynamicResource ControlOutlineUnselected}" />
<Setter Property="SnapsToDevicePixels" Value="true" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Border Padding="{TemplateBinding Padding}" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<!-- ANIMATIONS -->
<EventTrigger RoutedEvent="Button.MouseEnter">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation To="{DynamicResource BackgroundGreyLevel2Color}" Storyboard.TargetProperty="(Border.BorderBrush).Color" Duration="0:0:0.1"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Style.Triggers>
</Style>
カラーリソースがここで定義されています
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
...
<SolidColorBrush x:Key="BackgroundGreyLevel2" Color="#FF464646" />
<SolidColorBrush x:Key="TextParagraphWhiteP1" Color="White" />
<Color x:Key="BackgroundGreyLevel2Color" >#FF464646</Color>
...
</ResourceDictionary>
もちろん、私はインラインにフェードする色を書くことができますが、スタイルが灰色から例えば灰色に更新されているかのように、私が既に定義したリソースを使用することをはるかに好むでしょう。青;私はここと他のすべてのインラインの場所でそれを変更することを忘れないでください。
djomlastic、ご協力いただきありがとうございます。 「色資源を再定義」することで、あなたの最初の提案の意味を聞いてもいいですか? –
@BenHayward Well、BackgroundGreyLevel2Colorを他の色(赤など)に変更しても、キーを変更しないと、ボタンは赤色にアニメーション表示されます。 StaticResourceとDynamicResourceの問題は残っているので、ライブラリのユーザーがBackgroundGreyLevel2Colorをgold(灰色ではない)にしたいと判断した場合、ボタンは灰色にアニメーション表示されます。私は第二の選択肢が良いかもしれないと信じています、私はコントロールライブラリでそれを見ました。 – djomlastic
@djomalstic私は - 単にStoryboardをTo = "{StaticResource BackgroundGreyLevel2Color}"に変更しようとしましたが、これも機能しませんでした。私はあなたの2番目の提案の意味を理解しています。これが本当に最善の回避策かもしれません。私はそれを与えるだろう!多くのありがとう –