【问题标题】:How to use styles from separate xaml files如何使用来自不同 xaml 文件的样式
【发布时间】:2012-12-26 04:59:42
【问题描述】:

我有一个列出一组颜色的 styles.xaml 文件。这些颜色定义了应用程序某一部分中某些元素的显示方式,因此可以通过转换器使用。

我想在应用程序的另一部分创建这些颜色的图例,并有一个切换按钮列表,我想将背景颜色设置为 styles.xaml 中定义的颜色。

我是否需要以某种方式将styles.xaml 文件包含到定义切换按钮的xaml 文件中?或者有什么方法可以直接绑定到这些颜色值?

【问题讨论】:

    标签: c# wpf xaml binding


    【解决方案1】:

    将styles.xaml 添加到App.xaml

     <Application.Resources>
        <ResourceDictionary >
            <ResourceDictionary.MergedDictionaries >       
                <ResourceDictionary Source="styles.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
    

    【讨论】:

    • 试过这个,但我收到以下错误:添加到 IDictionary 的所有对象都必须具有 Key 属性或与之关联的其他类型的键。 styles.xaml 仅包含一个 resourceDictionary,所有元素都有键(元素的子元素除外)。
    • 向“styles.xaml”中的样式添加键。或者“styles.xaml”也应该是
    • 是的,您需要在 styles.xaml 中为所有颜色设置 x:Key
    • 对于我来说,xaml 是一个用户控件,所以在 我有一个带有 x:Key 的样式。在这种风格下,我有 和 MergedDictionaries 代码,如上面的答案所示。这是“合法”还是“正确”,或者为什么我仍然无法让它发挥作用?
    • @Chris Schaller - 考虑使用stackoverflow.com/review/suggested-edits/9491974 的内容发布答案。关于您的编辑的元问题meta.stackoverflow.com/questions/305966/…
    【解决方案2】:

    注意以下内容/答案的署名应转到@Chris Schaller。这个答案的内容最初是作为@chameleon86 answer 的编辑发布的,并且是rejected(另请参阅this 元)。但是我认为,这是一些有价值的内容,所以我正在“转发”它。

    要使 styles.xaml 中的定义可用于应用内的所有 XAML,请将 styles.xaml 添加到 App.xaml

    <Application.Resources>
       <ResourceDictionary >
            <ResourceDictionary.MergedDictionaries >  
                <ResourceDictionary Source="styles.xaml"/>
            </ResourceDictionary.MergedDictionaries>
            <!-- You can declare additional resources before or after Merged dictionaries, but not both -->
            <SolidColorBrush x:Key="DefaultBackgroundColorBrush" Color="Cornsilk" />
            <Style x:Key="DefaultBackgroundColor" TargetType="TextBox">
                <Setter Property="Background" Value="{StaticResource DefaultBackgroundColorBrush}" />
            </Style>
        </ResourceDictionary>
    </Application.Resources>
    

    要了解其工作原理,在运行时,您的窗口、页面或控件将作为正在运行的应用程序可视化树的子元素存在。

    您最初的问题是:

    “这些颜色定义了应用程序的一部分中的某些元素如何...”

    如果您只需要这些样式资源可用于一些 xaml 页面或窗口,而不是全部,那么您仍然可以使用此模式来合并窗口或网格的本地资源或其他直接控制。

    • 请注意,这样做会使这些样式仅可用于您声明资源字典的元素的子元素。

    看看将样式引用限定为单个网格以供使用是多么简单:

    <Grid>
        <Grid.Resources>
            <ResourceDictionary >
                <ResourceDictionary.MergedDictionaries >       
                    <ResourceDictionary Source="styles.xaml"/>
                </ResourceDictionary.MergedDictionaries>
                <!-- You can declare additional resources before or after Merged dictionaries, but not both -->
            </ResourceDictionary>
        </Grid.Resources>
        <!--
            Grid Content :)
          -->
    </Grid>
    

    【讨论】:

      猜你喜欢
      • 2023-03-22
      • 2021-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-11
      • 1970-01-01
      • 2010-12-19
      相关资源
      最近更新 更多