【问题标题】:WPF Custom Control Template Not Applied未应用 WPF 自定义控件模板
【发布时间】:2010-11-13 18:05:36
【问题描述】:

我确信这个问题或其衍生问题已经被问了无数次,但我找不到任何可以帮助我解决问题的东西,所以我在问。请随时将我引导到我确定存在但我找不到的副本。显然我不太擅长关键字。

我有一个自定义控件,它有自己的资源字典,仅用于定义控件模板。然后将该字典合并到 Generic.xaml 中。

问题是当这个控件出现在 UI 中时,它里面什么都没有。我用Snoop 找到了答案。控件在 UI 中,但它完全是空的。

您会在下面找到我认为导致该问题的项目。非常感谢您提供的任何帮助或建议。

我的文件夹结构的相关部分是这样的:

BasicTemplate.xaml:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WPFSpecBuilder.Layouts.Templates">

    <Style TargetType="{x:Type local:BasicTemplate}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:BasicTemplate}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <Grid>
                            <TextBlock Text="This is a basic template." />
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

Generic.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Layouts/Templates/XAML/BasicTemplate.xaml" />
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

【问题讨论】:

  • 我会通过排除原则推进:首先断言您的样式定义不是问题的一部分。因此,从嵌套的 ResourceDictionary 中挑选出来并将其粘贴到 Generic.xaml(我希望您在 App.xaml 中引用它)。之后显示所需的效果,尝试史蒂夫建议(断言您的相对路径设置正确)

标签: wpf custom-controls controltemplate resourcedictionary


【解决方案1】:

试试这个:

  1. 将构建操作设置为 BasicTemplate.xaml 到页面。

  2. 在 Generic.xaml 中添加对 BasicTemplate.xaml 的引用:

    ResourceDictionary Source="/WPDSpecBuilder;component/Layouts/Templates/Xaml/BasicTemplate.xaml"

它应该可以工作。

【讨论】:

    【解决方案2】:

    我认为这可能就像更改合并字典的相对路径一样简单。尝试在文件夹路径的开头添加/

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/Layouts/Templates/XAML/BasicTemplate.xaml" />
    </ResourceDictionary.MergedDictionaries>
    

    【讨论】:

      【解决方案3】:

      试试:

      <ResourceDictionary.MergedDictionaries>
           <ResourceDictionary Source="pack://application:,,,/WPDSpecBuilder;component/Layouts/Templates/XAML/BasicTemplate.xaml" />
      </ResourceDictionary.MergedDictionaries>
      

      See here for more details on Pack Uri's

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-09-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-28
        相关资源
        最近更新 更多