简体   繁体   English

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

[英]Creating a custom WPF control and set a DataTemplate as a Dependency Property

I'm creating a new control for my WPF application. 我正在为WPF应用程序创建一个新控件。 In it, I have added two dependency properties. 在其中,我添加了两个依赖项属性。 These ones is to give style a listbox with a bullet decorator. 这些是为样式提供一个带有项目符号装饰器的列表框。

Here is what I have: 这是我所拥有的:

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));
}

Then I've got the generic style here. 然后,我在这里有了通用样式。 Please, watch the last style I've set the PossibleAnswers to the listbox and the binding is okay. 请观看最后一种样式,我已将“可能的答案”设置为列表框,并且绑定正常。 But at the momment to pass the data template (this one is setted to the content control called ContentText ) and watch I've set the template called PossibleAnswersTemplate and it's wrong at this part. 但是在传递数据模板的时候(将其设置为名为ContentText的内容控件),然后看一下我已经设置了名为PossibleAnswersTemplate的模板,这是错误的。

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

Does not compile. 不编译。 What should be the error? 应该是什么错误?

<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>
 <ListBox ItemsSource="{TemplateBinding PossibleAnswers}" Tag="{TemplateBinding PossibleAnswersTemplate}" SelectionMode="Multiple"
                             Style="{StaticResource KinectRadioList}"/>

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

Your Template is of ListBox and TemplateBinding is Trying to find PossibleAnswersTemplate in ListBox and hence it is not working. 您的模板是ListBox,而TemplateBinding试图在ListBox中找到可能的AnswersTemplate,因此它不起作用。 I hope this will help. 我希望这将有所帮助。

EDIT: This is another solution 编辑:这是另一个解决方案

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM