Secondo this post, che l'animazione fantasia è costruito in e per rimuoverlo, si sta andando ad avere bisogno di ignorare il ControlTemplate
per il vostro Button
. Fortunatamente, non è troppo difficile. Ho usato this post come materiale di partenza e ho trovato il seguente Style
che ti dà l'idea.
<Style x:Key="MouseOverButtonStyle" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<ControlTemplate.Resources>
<Style x:Key="ShadowStyle">
<Setter Property="Control.Foreground" Value="LightGray" />
</Style>
</ControlTemplate.Resources>
<Border Name="border" BorderThickness="1" Padding="4,2" BorderBrush="DarkGray" CornerRadius="3" Background="{TemplateBinding Background}">
<Grid >
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Name="contentShadow" Style="{StaticResource ShadowStyle}">
<ContentPresenter.RenderTransform>
<TranslateTransform X="1.0" Y="1.0" />
</ContentPresenter.RenderTransform>
</ContentPresenter>
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Name="content"/>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Beige" />
</Trigger>
</Style.Triggers>
</Style>
Aggiornamento: Se sei morto insieme su come applicare il Style
nel codice e non si desidera utilizzare una (probabilmente il modo migliore per farlo) ResourceDictionary
, è possibile caricare i Style
dinamicamente utilizzando XamlReader.Load
:
const string xaml = @"
<Style xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
TargetType='Button'>
<Setter Property='Template'>
<!--- Omitted For Clarity --->
</Setter>
<Style.Triggers>
<Trigger Property='IsMouseOver' Value='True'>
<Setter Property='Background' Value='Beige' />
</Trigger>
</Style.Triggers>
</Style>";
var encoding = new ASCIIEncoding();
var bytes = encoding.GetBytes(xaml);
var style = (Style)XamlReader.Load(new MemoryStream(bytes));
Button1.Style = style;
fonte
2010-04-22 01:08:01
So che posso farlo tramite xaml ma non può essere fatto tramite C# ??? – Luiscencio
È possibile inserire lo stile in un dizionario risorse e quindi estrarlo quando necessario per applicare al pulsante. –
+1 Proverò il tuo aggiornamento, sembra promettente grazie – Luiscencio