我已经在 resorces.xaml 中编写了这段代码,以便在我的项目中的所有按钮上应用它:
<Style TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Rectangle x:Name="rect" Style="{DynamicResource rectangle_style}" Cursor="Hand">
<Rectangle.Fill>
<ImageBrush ImageSource="Attempts\image.jpg" Stretch="UniformToFill"/>
</Rectangle.Fill>
</Rectangle>
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Content=""/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsFocused" Value="True"/>
<Trigger Property="IsDefaulted" Value="True"/>
<Trigger Property="IsMouseOver" Value="True"/>
<Trigger Property="IsPressed" Value="True"/>
<Trigger Property="IsEnabled" Value="False"/>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我会从 C# 后面的代码中编辑这种样式,因为我想从代码中动态更改图像源,我如何从 C# 后面的代码中编写它?