简体   繁体   English

自定义控件中Style类型的DependencyProperty

[英]DependencyProperty of type Style in custom control

In a custom control I'd like to use a Style (given as DependencyProperty) for a TextBlock in my Template. 在自定义控件中,我想对模板中的TextBlock使用样式(称为DependencyProperty)。

MyControl.cs MyControl.cs

public static DependencyProperty HeadingStyleProperty =
    DependencyProperty.Register("HeadingStyle",
                                typeof (Style),
                                typeof (MyControlElement),
                                new PropertyMetadata(new Style(typeof(TextBlock))));

public Style HeadingStyle {
    get { return (Style) GetValue(HeadingStyleProperty); }
    set { SetValue(HeadingStyleProperty, value); }
}

MyControl.xaml MyControl.xaml

<ResourceDictionary ...>
    <Style TargetType="local:MyControl">
        <Style.Resources>
            <!-- Getting error on BasedOn="TemplateBinding -->
            <Style TargetType="TextBlock" BasedOn="{TemplateBinding HeadingStyle}" x:Key="Heading" />
        </Style.Resources>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <TextBlock Style="{StaticResource Heading}" Text="StyledHeading" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

I get compiler error 'HeadingStyle' member is not valid because it does not have a qualifying type name. 我收到编译器错误'HeadingStyle'成员无效,因为它没有合格的类型名称。

Do I have to modify the DataType of my DP from Style to a more specific one? 我是否需要将DP的数据类型从“样式”修改为更具体的数据类型? Or what causes this error. 还是导致此错误的原因。 The initial value of DP is set to Style for TargetType TextBlock... DP的初始值设置为TargetType TextBlock的样式...

First of all, Style does not derive from DependencyObject , so you cannot set any binding on it. 首先, Style 不是DependencyObject派生的,因此您不能对其设置任何绑定。

If you want your TextBlock (which is part of the template) to be styled by your property, simply set the TemplateBinding on its Style property directly (what would be the purpose of the style you defined as a resource anyway?). 如果要通过属性设置TextBlock (模板的一部分)的样式,只需直接在其Style属性上设置TemplateBinding (无论如何,将样式定义为资源的目的是什么?)。 This can be done in two ways. 这可以通过两种方式完成。 One is to use fully qualified property name: 一种是使用完全限定的属性名称:

<ControlTemplate>
    <TextBlock Style="{TemplateBinding local:MyControl.HeadingStyle}" (...) />
</ControlTemplate>

The other way, more commonly used, is to use simplified property name, but it requires the ControlTemplate.TargetType to be specified: 另一种更常用的方法是使用简化的属性名称,但是它需要指定ControlTemplate.TargetType

<ControlTemplate TargetType="{x:Type local:MyControl}">
    <TextBlock Style="{TemplateBinding HeadingStyle}" (...) />
</ControlTemplate>

The second method works because the XAML parser is smart enough to know how to resolve the HeadingStyle property - if you didn't specify ControlTemplate.TargetType , you'd get the exact same error you're getting right now. 第二种方法之所以有效,是因为XAML解析器足够聪明,足以知道如何解析HeadingStyle属性-如果您未指定ControlTemplate.TargetType ,则将得到与现在完全相同的错误。

You could of course trick the compiler to compile your code by using fully qualified property name: 您当然可以通过使用完全限定的属性名来欺骗编译器来编译代码:

<Style x:Key="Heading" BasedOn="{TemplateBinding local:MyControl.HeadingStyle}" (...) />

but that would result in a runtime error saying that TemplateBindingExpression cannot be converted to Style . 但这会导致运行时错误,指出TemplateBindingExpression无法转换为Style

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WPF自定义控件:Collection类型的DependencyProperty - WPF Custom Control: DependencyProperty of Collection type 自定义控件中的DependencyProperty问题 - Problem with DependencyProperty in custom control 具有DependencyProperty的自定义控件的Designer行为 - Designerbehavior of custom control with DependencyProperty 将自定义控件中的泛型类型 ItemsControl ItemsSource 绑定到 DependencyProperty - Binding generic type ItemsControl ItemsSource in custom control to DependencyProperty 自定义控件上的模板绑定到DependencyProperty无效 - TemplateBinding to DependencyProperty on a custom control not working 使用TemplateBinding的样式触发器中的自定义DependencyProperty - Custom DependencyProperty in style triggers using TemplateBinding WPF自定义控件:将CollectionViewSource绑定到DependencyProperty - WPF Custom Control: Bind CollectionViewSource to DependencyProperty 将自定义控件中的DependencyProperty绑定到ViewModel属性 - Binding DependencyProperty in custom control to ViewModel property 将 ControlTemplate 子项绑定到自定义控件上的 DependencyProperty 不起作用 - Binding ControlTemplate child to DependencyProperty on a custom control not working WPF:DependencyProperty在TextBlock上有效,但不适用于自定义控件 - WPF: DependencyProperty works on TextBlock but not on custom control
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM