c#代码使用ResourceDictionary样式

对于ResourceDictionary样式代码:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
...
 <Style x:Key="AlertButton" TargetType="ButtonBase" BasedOn="{StaticResource SystemButtonBase}">
        <Setter Property="Cursor" Value="Hand" />
        <Setter Property="Margin" Value="8"/>
        <Setter Property="Padding" Value="4"/>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Cursor" Value="Hand"></Setter>
                <Setter Property="Opacity" Value=".8" />
            </Trigger>
            <Trigger Property="IsPressed" Value="True">
                <Setter Property="Opacity" Value=".4" />
            </Trigger>
        </Style.Triggers>
    </Style>
...
</ResourceDictionary>

使用C#代码引用:

            ResourceDictionary mystyles;
            mystyles = new ResourceDictionary();
            mystyles.Source = new Uri("/FirstFloor.ModernUI;component/Assets/Button.xaml",
                    UriKind.RelativeOrAbsolute);
            System.Windows.Style btnStyle = mystyles["AlertButton"] as Style;

        new Button
                {
                    Foreground = new SolidColorBrush(Colors.White),
                    Content = content,
                    Command = this.CloseCommand,
                    CommandParameter = result,
                    IsDefault = false,
                    IsCancel = isCancel,
                    MinHeight = 21,
                    MinWidth = 65,
                    Background = new SolidColorBrush(System.Windows.Media.Color.FromRgb(252, 73, 0)),
                    Margin = new Thickness(4, 0, 4, 0)
                        ,
                    Style = new Style(typeof(Button), btnStyle) { Triggers = { tRed } }
                };

 

 

 

 

转载于:https://www.cnblogs.com/9527y/p/3819862.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,针对你的问题,我来简单介绍一下Style样式的四种使用方法,以及用C#代码动态加载资源文件并设置样式的方法。 1. 在XAML中定义Style 在XAML中,我们可以使用Style标签来定义样式,然后在控件中引用该样式即可。 ```xml <Window.Resources> <Style x:Key="myButtonStyle" TargetType="Button"> <Setter Property="Background" Value="Red"/> <Setter Property="Foreground" Value="White"/> <Setter Property="FontSize" Value="12"/> </Style> </Window.Resources> <Button Content="Click Me" Style="{StaticResource myButtonStyle}"/> ``` 2. 直接在控件中定义Style 除了在XAML中定义Style外,我们还可以在控件中直接定义Style。 ```xml <Button Content="Click Me"> <Button.Style> <Style TargetType="Button"> <Setter Property="Background" Value="Red"/> <Setter Property="Foreground" Value="White"/> <Setter Property="FontSize" Value="12"/> </Style> </Button.Style> </Button> ``` 3. 在资源字典中定义Style 我们还可以将Style定义在资源字典中,以便在整个应用程序中重复使用。 ```xml <Application.Resources> <ResourceDictionary> <Style x:Key="myButtonStyle" TargetType="Button"> <Setter Property="Background" Value="Red"/> <Setter Property="Foreground" Value="White"/> <Setter Property="FontSize" Value="12"/> </Style> </ResourceDictionary> </Application.Resources> <Button Content="Click Me" Style="{StaticResource myButtonStyle}"/> ``` 4. 动态加载Style 最后,我们可以使用C#代码动态加载资源文件并设置样式。 ```csharp var resourceUri = new Uri("/MyAssembly;component/Styles.xaml", UriKind.Relative); var resource = Application.LoadComponent(resourceUri) as ResourceDictionary; var style = resource["myButtonStyle"] as Style; myButton.Style = style; ``` 以上就是关于Style样式的四种使用方法,以及用C#代码动态加载资源文件并设置样式的介绍。希望能够对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值