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");
        }
posted @ 2016-10-02 22:53  天涯海角路  阅读(273)  评论(0)    收藏  举报