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