简体   繁体   中英

How to set Button.Style to x:Static resource from code behind in WPF?

I need to call static resource for button style in code-behind using C#

This is my Button in XAML:

<Button Height="48" HorizontalAlignment="Left" Margin="229,0,0,0" Name="btnlogin" 
        VerticalAlignment="Top" Width="90" Grid.Row="6" BorderThickness="0" 
        MouseEnter="btnlogin_MouseEnter" MouseLeave="btnlogin_MouseLeave" 
        BorderBrush="Transparent" 
        Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}">
    <Button.Background>
        <ImageBrush ImageSource="/BaccaratDealerInterfaceWPF;component/Images/Login_Button_Normal.png" />
    </Button.Background>
</Button>

I need to set this Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" to btnlogin.Style property in code behind C#.

I've tried

 Style style = this.FindResource("{x:Static ToolBar.ButtonStyleKey}") as Style;
 btnlogin.Style = style;

Try

this.FindResource(ToolBar.ButtonStyleKey);

Here's the Microsoft Documentation for this style and how to access it in both:

  • Code behind ToolBar.ButtonStyleKey
  • XAML <object property="{x:Static ToolBar.ButtonStyleKey}"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM