模板化后,自定义ContentControl不显示内容?

时间:2011-02-25 19:31:27

标签: c# .net wpf templates xaml

我创建了一个名为DataGridInsertRowPresenter的新自定义控件,它继承自ContentControl。一切都运作良好。

然后我添加了更改模板的新样式,它不再显示内容。这是控件:

public class DataGridInsertRowPresenter : ContentControl {
    static DataGridInsertRowPresenter() {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(DataGridInsertRowPresenter), new FrameworkPropertyMetadata(typeof(DataGridInsertRowPresenter)));
    }
}

这是我的模板:

<Style TargetType="{x:Type Primitives:DataGridInsertRowPresenter}" BasedOn="{StaticResource {x:Type ContentControl}}" >
    <Setter Property="Template" >
        <Setter.Value>
            <ControlTemplate>
                <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我的代码出了什么问题?

2 个答案:

答案 0 :(得分:0)

可能是你的assemblyinfo.cs缺少一个属性来告诉它在哪里寻找资源。检查assemblyinfo.cs中是否包含以下代码:

[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page, 
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page, 
// app, or any theme specific resource dictionaries)

)]

确保你的风格在Themes \ Generic.xaml中(或作为合并字典引用)

答案 1 :(得分:0)

您需要向TargetType添加ControlTemplate,因为Control没有名为Content的媒体资源:

<ControlTemplate TargetType="{x:Type Primitives:DataGridInsertRowPresenter}">