我有一个Grid
我实际上用作按钮,我需要在鼠标按钮关闭时将背景渐变更改为另一个渐变。释放鼠标按钮后,将其更改回原始颜色。另外,我还需要执行一些操作。我试图在后面的代码中完成这个,但也许这可以在xaml中完成?我这样做是因为定制按钮以使我需要的外观和感觉变得更加困难。我怎么能这样做呢?
XAML:
<DockPanel LastChildFill="True" Width="40" Height="40" Margin="22,20,22,5">
<Border BorderThickness="0,1,0,0" DockPanel.Dock="Top" BorderBrush ="#747474" />
<Border BorderThickness="1,0,0,0" DockPanel.Dock="Left">
<Border.BorderBrush>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#747474" Offset="0"/>
<GradientStop Color="#464648" Offset="1"/>
</LinearGradientBrush>
</Border.BorderBrush>
</Border>
<Border BorderThickness="0,0,1,0" DockPanel.Dock="Right">
<Border.BorderBrush>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#747474" Offset="0"/>
<GradientStop Color="#464648" Offset="1"/>
</LinearGradientBrush>
</Border.BorderBrush>
</Border>
<Border BorderThickness="0,1,0,0" DockPanel.Dock="Bottom" BorderBrush ="#464648" />
<Grid Width="38" Height="38">
<Grid.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#585858" Offset="0"/>
<GradientStop Color="#464648" Offset="1"/>
</LinearGradientBrush>
</Grid.Background>
<Image HorizontalAlignment="Center" VerticalAlignment="Center" Source="/common/printer_20.png" Stretch="None" />
</Grid>
答案 0 :(得分:1)
你有几种方法可以达到这个目的。
Style
和视觉状态。问题:执行命令会很脏。Behavior
添加Grid
。分配鼠标事件并在代码中更改Background
。此外,您可以执行命令。Trigger
更改背景。使用MVVM Light执行Command
。您更喜欢哪种方式?