【问题标题】:Creating a custom WPF control and set a DataTemplate as a Dependency Property创建自定义 WPF 控件并将 DataTemplate 设置为依赖属性
【发布时间】:2012-07-24 15:17:42
【问题描述】:

我正在为我的 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 设置为列表框的最后一种样式,并且绑定没问题。但是现在要传递数据模板(这个设置为名为@9​​87654322@ 的内容控件)并注意我已经设置了名为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>

【问题讨论】:

    标签: c# wpf xaml styles dependency-properties


    【解决方案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}}}" />
    

    【讨论】:

    • 不,它没有用。输出抛出我:System.Windows.Data 错误:4:无法找到与引用'RelativeSource FindAncestor,AncestorType='System.Windows.Controls.UserControl',AncestorLevel='1'' 进行绑定的源。 BindingExpression:Path=PossibleAnswersTemplate;数据项=空;目标元素是'ContentControl'(名称='');目标属性是“ContentTemplate”(类型“DataTemplate”)
    • @DarfZon 的 AncestorType 可能是 local:QuestionControl
    • 将ListBox的Tag属性(TemplateBinding)绑定到PossibleAnswersTemplate,并将ContentControl的ContentTemplate属性绑定到ListBox(TemplateBinding)的Tag属性。我已经更新了答案。
    • @ethicallogics 你对之前的代码是正确的。只需按照狒狒的建议进行修改,它确实奏效了!谢谢大家
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多