创建自定义WPF控件并将DataTemplate设置为依赖项属性

时间:2012-07-24 15:17:42

标签: c# wpf xaml styles dependency-properties

我正在为我的WPF应用程序创建一个新控件。在其中,我添加了两个依赖项属性。这些是给一个带有子弹装饰器的列表框样式。

这就是我所拥有的:

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

    // Another dependency properties...

    // PossibleAnswers : IEnumerable
    public IEnumerable PossibleAnswers
    {
        get { return (IEnumerable)base.GetValue(PossibleAnswersProperty); }
        set { base.SetValue(PossibleAnswersProperty, value); }
    }

    public static readonly DependencyProperty PossibleAnswersProperty =
      DependencyProperty.Register("PossibleAnswers", typeof(IEnumerable), typeof(QuestionControl));

    // PossibleAnswers : DataTemplate
    public DataTemplate PossibleAnswersTemplate
    {
        get { return (DataTemplate)base.GetValue(PossibleAnswersTemplateProperty); }
        set { base.SetValue(PossibleAnswersTemplateProperty, value); }
    }

    public static readonly DependencyProperty PossibleAnswersTemplateProperty =
      DependencyProperty.Register("PossibleAnswersTemplate", typeof(DataTemplate), typeof(QuestionControl));
}

然后我在这里得到了通用风格。请观看我将PossibleAnswers设置到列表框的最后一种样式,绑定没问题。但是要传递数据模板(这个被设置为名为ContentText的内容控件)并观察我设置了名为PossibleAnswersTemplate的模板,这部分是错误的。

<ContentControl x:Name="ContentText" Margin="2 0 0 0" Content="{Binding}" ContentTemplate="{TemplateBinding PossibleAnswersTemplate}" />

不编译。应该是什么错误?

<Style TargetType="ListBox" x:Key="KinectRadioList">
    <Style.Setters>
        <Setter Property="ItemContainerStyle">
            <Setter.Value>
                <Style TargetType="{x:Type ListBoxItem}">
                    <Style.Setters>
                        <Setter Property="OverridesDefaultStyle" Value="True"/>
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type ListBoxItem}">
                                    <ControlTemplate.Resources>
                                        <Style TargetType="{x:Type Ellipse}">
                                            <Style.Setters>
                                                <Setter Property="Fill" Value="Black"/>
                                                <Setter Property="Stroke" Value="Black"/>
                                            </Style.Setters>
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding RelativeSource={x:Static RelativeSource.TemplatedParent}, Path=IsEnabled}" Value="False">
                                                    <Setter Property="Fill" Value="Black"/>
                                                    <Setter Property="Stroke" Value="Black"/>
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </ControlTemplate.Resources>
                                    <BulletDecorator Background="Transparent" >
                                        <BulletDecorator.Bullet>
                                            <Canvas Width="15" Height="15">
                                                <Ellipse Width="13" Height="13" Canvas.Left="1" Canvas.Top="1" StrokeThickness="1" Fill="{x:Null}"/>
                                                <Ellipse Width="8" Height="8" Canvas.Left="3.5" Canvas.Top="3.5" Stroke="{x:Null}" Visibility="{Binding RelativeSource={x:Static RelativeSource.TemplatedParent}, Path=IsSelected, Converter={StaticResource BoolToVisibilityConverter}}"/>
                                            </Canvas>
                                        </BulletDecorator.Bullet>
                                        <ContentControl x:Name="ContentText" Margin="2 0 0 0" Content="{Binding}" ContentTemplate="{TemplateBinding PossibleAnswersTemplate}" />
                                    </BulletDecorator>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style.Setters>
                </Style>
            </Setter.Value>
        </Setter>
    </Style.Setters>
</Style>


<Style TargetType="{x:Type local:QuestionControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:QuestionControl}">
                    <StackPanel>
                        <ContentControl Content="{TemplateBinding QuestionText}"
                                        ContentTemplate="{TemplateBinding QuestionTextTemplate}" />
                        <ListBox ItemsSource="{TemplateBinding PossibleAnswers}" SelectionMode="Multiple"
                                 Style="{StaticResource KinectRadioList}"/>
                    </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

1 个答案:

答案 0 :(得分:1)

 <ListBox ItemsSource="{TemplateBinding PossibleAnswers}" Tag="{TemplateBinding PossibleAnswersTemplate}" SelectionMode="Multiple"
                             Style="{StaticResource KinectRadioList}"/>

    <ContentControl ContentTemplate="{TemplateBinding Tag}"/>

你的模板是ListBox,而TemplateBinding试图在ListBox中找到PossibleAnswersTemplate,因此它无法正常工作。我希望这会有所帮助。

编辑:这是另一种解决方案

<ContentControl Margin="2 0 0 0" Content="{Binding}" ContentTemplate="{Binding PossibleAnswersTemplate, RelativeSource={RelativeSource AncestorType={x:Type local:QuestionControl}}}" />