【问题标题】:WPF and external CSS fileWPF 和外部 CSS 文件
【发布时间】:2009-09-18 07:29:01
【问题描述】:

是否可以将样式定义存储在一个文件 (XXXXX.css) 中,然后在 XAML 文件中使用该文件?

【问题讨论】:

    标签: c# .net css wpf xaml


    【解决方案1】:

    它不叫 CSS,但你可以像这样在单独的文件中创建资源字典:

    <ResourceDictionary 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    
        <Style x:Key="MyStyledButton" TargetType="Button">
            .
            Put the details of your style here, just like you would for a normal style.
            .
        </Style>
    
    </ResourceDictionary>
    

    然后您可以通过在类上设置资源来将该资源字典导入其他 XAML 文件,例如,如果您有一个 Window 类,您会这样做:

    <Window.Resources>
        <ResourceDictionary Source="MyStylesFile.xaml" />
    </Window.Resources>
    

    现在该样式对窗口中的任何控件都生效,就像您直接在窗口资源中指定样式一样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-10
      • 1970-01-01
      • 1970-01-01
      • 2018-04-27
      • 1970-01-01
      • 1970-01-01
      • 2018-04-25
      相关资源
      最近更新 更多