WPF XAML与CodeBehind中设置获取资源
<Window.Resources>
<!--<SolidColorBrush x:Key="staticBrush">SkyBlue</SolidColorBrush>-->
</Window.Resources>
<Grid>
<StackPanel>
<!--<Button Width="150" Height="50" Background="{StaticResource staticBrush}" Click="StaticBtn_Click">StaticResource</Button>-->
<Button x:Name="staticBtn" Width="150" Height="50" Click="StaticBtn_Click">StaticResource</Button>
<Button x:Name="dynamicBtn" Width="150" Height="50" Click="DynamicBtn_Click">DynamicResource</Button>
</StackPanel>
</Grid>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Resources.Add("staticBrush", new SolidColorBrush(Colors.Beige));
}
private void StaticBtn_Click(object sender, RoutedEventArgs e)
{
this.staticBtn.Background = (Brush)staticBtn.FindResource("staticBrush");
}

浙公网安备 33010602011771号