0
我正在嘗試編寫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好吧,如果您將BackgroundGreyLevel2Color更改爲某種其他顏色(如紅色),但您不更改該按鍵,則您的按鈕將變爲紅色。 StaticResource vs DynamicResource的問題依然存在,所以如果你的圖書館的用戶決定他希望BackgroundGreyLevel2Color是黃金(而不是灰色),那麼該按鈕將被動畫爲灰色。我相信第二種選擇可能會更好,我已經在控制庫中看到了。 – djomlastic
@djomalstic我看到了 - 我試着簡單地將故事板改爲To =「{StaticResource BackgroundGreyLevel2Color}」,但這也行不通。我明白你的第二個建議是什麼意思 - 這可能確實是最好的解決方法。我會放棄它!非常感謝 –