110,534
社区成员
发帖
与我相关
我的任务
分享
<Window x:Class="ResourceTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="600" Width="600">
<StackPanel>
<StackPanel.Resources>
<Style TargetType="Button" x:Key="GelButton">
<Setter Property="Content" Value="Daima"/>
<Setter Property="Margin" Value="1,2,1,2"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="Background" Value="Yellowgreen"/>
</Style>
</StackPanel.Resources>
<Button
Height="50" Width="50" Content="Button 1" Click="Button_Click" />
</StackPanel>
</Window>
private void Button_Click(object sender, RoutedEventArgs e)
{
Button b = sender as Button;
b.Style = this.FindResource("GelButton") as Style;
}
[code=csharp]
<Window.Resources>
<Style TargetType="Button" x:Key="GelButton">
<Setter Property="Content" Value="Daima"/>
<Setter Property="Margin" Value="1,2,1,2"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="Background" Value="Yellowgreen"/>
</Style>
</Window.Resources>
<Button
Height="50" Width="50" Content="Button 1" Click="Button_Click" style="StaticResource GelButton"/>
[/code]