如何更改按钮单击中的动态资源?

时间:2022-11-21 15:27:34

I am new to WPF i need to learn the static resources and dynamic resources, i have tried to set the Style for Button using Dynamic Resource and change the style using button click but style is not changed, please refer my below code i used for change the button style using click,

我是WPF的新手,我需要学习静态资源和动态资源,我尝试使用动态资源设置按钮样式并使用按钮单击更改样式但样式未更改,请参考我用于更改的下面的代码点击按钮样式,

<Window.Resources>
    <Style x:Key="Style1" TargetType="Button">
        <Setter Property="Background" Value="Red" />
    </Style>
</Window.Resources>  
<Button x:Name="Content" Content="Content" Height="30" Width="200" Style="{DynamicResource ResourceKey=Style1}" Click="Content_Click" />   

Click :

点击:

 private void Content_Click(object sender, RoutedEventArgs e)
 {
   this.Resources.Add("Style1", new SolidColorBrush(Colors.Blue));
 }    

Could you please any one guide me how to achieve my requirement?

你能不能请任何人指导我如何达到我的要求?

3 个解决方案

#1


0  

Add like this hence you can overwrite the Style1 with the new style:

添加如下,因此您可以使用新样式覆盖Style1:

private void Content_Click(object sender, RoutedEventArgs e)
 {
       Style style = new Style{ TargetType = typeof(Button)};
       style.Setters.Add(new Setter(Button.BackgroundProperty, Brushes.Blue));
       this.Resources["Style1"] = style;
 }

#2


0  

To gain access to the Resource of the code must identify them in the file App.xaml:

要获得对代码资源的访问权限,必须在App.xaml文件中标识它们:

<Application.Resources>
    <SolidColorBrush x:Key="Style1" />
</Application.Resources>

The Resource can be changed in code line of the form:

可以在表单的代码行中更改资源:

Application.Current.Resources["MyResource"] = MyNewValue;

Application.Current.Resources [“MyResource”] = MyNewValue;

private void Content_Click(object sender, RoutedEventArgs e)
 {
    SolidColorBrush MyBrush = Brushes.Blue;

    // Set the value
    Application.Current.Resources["DynamicBG"] = MyBrush;
}

#3


0  

If you are learning DynamicResource, the correct way would be

如果您正在学习DynamicResource,那么正确的方法就是

Xaml:

XAML:

<Window.Resources>        
        <SolidColorBrush x:Key="DynamicBG" Color="Red"/>         
</Window.Resources>    

<Button x:Name="button" Content="Content" Height="30" Width="200" Background="{DynamicResource DynamicBG}" Click="Content_Click"/>

Code Behind:

代码背后:

private void Content_Click(object sender, RoutedEventArgs e)
{
   this.Resources["DynamicBG"] = new SolidColorBrush(Colors.Blue);
}

Additional Note:

附加说明:

You can see that, on changing the Button with Background="{StaticResource DynamicBG}"will not change the Color on Buttonclick. That's where the Dynamic key play its role.

您可以看到,在使用Background =“{StaticResource DynamicBG}”更改Button时,不会更改Buttonclick上的Color。这就是Dynamic键发挥作用的地方。

#1


0  

Add like this hence you can overwrite the Style1 with the new style:

添加如下,因此您可以使用新样式覆盖Style1:

private void Content_Click(object sender, RoutedEventArgs e)
 {
       Style style = new Style{ TargetType = typeof(Button)};
       style.Setters.Add(new Setter(Button.BackgroundProperty, Brushes.Blue));
       this.Resources["Style1"] = style;
 }

#2


0  

To gain access to the Resource of the code must identify them in the file App.xaml:

要获得对代码资源的访问权限,必须在App.xaml文件中标识它们:

<Application.Resources>
    <SolidColorBrush x:Key="Style1" />
</Application.Resources>

The Resource can be changed in code line of the form:

可以在表单的代码行中更改资源:

Application.Current.Resources["MyResource"] = MyNewValue;

Application.Current.Resources [“MyResource”] = MyNewValue;

private void Content_Click(object sender, RoutedEventArgs e)
 {
    SolidColorBrush MyBrush = Brushes.Blue;

    // Set the value
    Application.Current.Resources["DynamicBG"] = MyBrush;
}

#3


0  

If you are learning DynamicResource, the correct way would be

如果您正在学习DynamicResource,那么正确的方法就是

Xaml:

XAML:

<Window.Resources>        
        <SolidColorBrush x:Key="DynamicBG" Color="Red"/>         
</Window.Resources>    

<Button x:Name="button" Content="Content" Height="30" Width="200" Background="{DynamicResource DynamicBG}" Click="Content_Click"/>

Code Behind:

代码背后:

private void Content_Click(object sender, RoutedEventArgs e)
{
   this.Resources["DynamicBG"] = new SolidColorBrush(Colors.Blue);
}

Additional Note:

附加说明:

You can see that, on changing the Button with Background="{StaticResource DynamicBG}"will not change the Color on Buttonclick. That's where the Dynamic key play its role.

您可以看到,在使用Background =“{StaticResource DynamicBG}”更改Button时,不会更改Buttonclick上的Color。这就是Dynamic键发挥作用的地方。