是否可以在WPF中设置BulletDecorator的样式?

Jac*_*eja 8 wpf styles

显然它可以应用一种样式 - 我试图找出它是否可以在样式中定义Bullet元素,因此你不必在xaml中反复定义它

<BulletDecorator>
       <BulletDecorator.Bullet>
            ...my bullet UIElement here...
        </BulletDecorator.Bullet>
        <TextBlock>
            ... my text here...
        </TextBlock>
</BulletDecorator> 
Run Code Online (Sandbox Code Playgroud)

Ray*_*rns 13

BulletDecorator.Bullet无法设置样式,并且BulletDecorator不是Control,因此无法模板化.

但是,您可以通过为ContentControl定义ControlTemplate来获得纯XAML中的效果,如下所示:

<ControlTemplate x:Key="BulletTemplate" TargetType="{x:Type ContentControl}">
  <BulletDecorator>
    <BulletDecorator.Bullet>
      ...my bullet UIElement here...
    </BulletDecorator.Bullet>
    <ContentPresenter />
  </BulletDecorator>
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)

现在您可以像这样使用它:

<ContentControl Template="{StaticResource BulletTemplate}">
  <TextBlock />
</ContentControl>
Run Code Online (Sandbox Code Playgroud)

如果你只使用它几次,"<ContentControl Template = ..."技术工作正常.如果您打算更频繁地使用它,可以定义一个MyBullet类:

public class MyBullet : ContentControl
{
  static MyBullet()
  {
    DefaultStyleKey.OverrideMetadata(typeof(MyBullet), new FrameworkPropertyMetadata(typeof(MyBullet));
  }
}
Run Code Online (Sandbox Code Playgroud)

然后将您的ControlTemplate移动到Theme/Generic.xaml(或合并到其中的字典)并将其包装为:

<Style TargetType="{x:Type local:MyBullet}">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate
        ...
    </Setter.Value>
  </Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)

如果这样做,您可以使用:

<local:MyBullet>
  <TextBox />
</local:MyBullet>
Run Code Online (Sandbox Code Playgroud)

在您的应用程序中的任