2

我正在为我的 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>
4

1 回答 1

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}}}" />
于 2012-07-24T15:44:14.520 回答