【问题标题】:WPF Style template with recource dictionary带有资源字典的 WPF 样式模板
【发布时间】:2015-01-14 15:22:49
【问题描述】:

我对此很陌生,所以也许这是一个非常简单的事情,可能是错误的......

我尝试使用样式 ResourceDictionary 但是当我尝试在我的应用程序中使用它时它不起作用。 这是目录:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<LinearGradientBrush x:Key ="DarkBackground" StartPoint ="0,0" EndPoint =" 1,1">
    <GradientStop Offset="0" Color =" #FF333344"></GradientStop>
    <GradientStop Offset="1" Color =" #FF666677"></GradientStop>
</LinearGradientBrush>

<LinearGradientBrush x:Key="StandardBackground" EndPoint="0,1" StartPoint="0,0">
    <GradientStop Color="#FFF3F3F3" Offset="0"/>
    <GradientStop Color="#FFEBEBEB" Offset="0.5"/>
    <GradientStop Color="#FFDDDDDD" Offset="0.5"/>
    <GradientStop Color="#FFBBBBBB" Offset="1"/>
</LinearGradientBrush>

Application.xaml 看起来像这样:

<Application x:Class="MyApplication.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="\src\GUI\MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary Source ="StyleTemplates.xaml"> </ResourceDictionary>
    </Application.Resources>
</Application>

主窗口中的条目:

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

在我的 MainWindow 中,我想做类似的事情:

<TabItem Header="Project config" Background="{StaticResource StandardBackground}" Margin="-2,-2,2,0" IsEnabled="{Binding ToolPreference.ProjectLoaded}">

接下来我必须做什么才能在我的 MainWindow.xaml 以及所有其他可用窗口中将样式作为静态资源获取?

在我的 MainWindow.xaml 中,我现在可以在尝试此操作时看到“StandardBackground”:

<TabItem Header="Project config" Background="{StaticResource x:StandardBackground}" Margin="-2,-2,2,0" IsEnabled="{Binding ToolPreference.ProjectLoaded}">

当我添加“x:”时,我会看到一个带有 StandardBackground 和 DarkBackground 的下拉菜单。但我收到错误:(从德语翻译成英语)“资源:x:StandardBackground 无法解析”

【问题讨论】:

  • 你必须像这样合并 ResourceDictionary prntscr.com/5sdssx
  • 好的,我试过了,还是不行……
  • Tabitem 背景,您可以通过编辑其样式来更改。请参阅prntscr.com/5se761

标签: c# wpf xaml templates


【解决方案1】:

您的 XAML 没有任何问题。我怀疑您的 StandardBackground 和 DarkBackground 资源可以在其他控件上正常工作。在这种情况下,您的问题是 TabItem 的 Background 属性无效。您需要研究 TabControl 的样式以实现您想要的外观。

要验证您的样式是否至少可以访问,您可以尝试以下 XAML 更改:

<TabItem>
    <TabItem.Header>
        <TextBox Text="Project config" Background="{StaticResource StandardBackground}" />
    </TabItem.Header>
</TabItem>

【讨论】:

  • 它在文本框上运行良好。我会看看 TabContol 特定的背景...非常感谢。
【解决方案2】:

试试

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

【讨论】:

  • 我试过了,但还是不行,我可能需要在“MainWindow.xaml”中添加一些东西吗?!
  • 除非需要使用多个字典,否则不需要合并字典。
  • @Andreas 抱歉,您的问题对我来说不够清楚。
猜你喜欢
  • 1970-01-01
  • 2011-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多