2

全部,我正在开发我们的自定义控件,该控件具有Border以下Background设置

<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
    <GradientStop Color="#FFF7F7F7" Offset="0.164" />
    <GradientStop Color="#FFDBDBDB" Offset="0.664" />
    <GradientStop Color="#FFB3B3B3" Offset="0.978" />
</LinearGradientBrush>

但是,我想允许用户覆盖它。我创建了 type 的依赖属性Brush,但是我无法弄清楚如何绑定到背景,同时在 XAML 中仍然具有此默认值。这甚至可能吗?

4

1 回答 1

2

看看TemplateBinding 标记扩展

<Style TargetType="{x:Type your:YourControl}">
    <Setter Property="Background">
        <Setter.Value>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="#FFF7F7F7" Offset="0.164" />
                <GradientStop Color="#FFDBDBDB" Offset="0.664" />
                <GradientStop Color="#FFB3B3B3" Offset="0.978" />
            </LinearGradientBrush>
        </Setter.Value>
    </Setter>

然后在您的默认 ControlTemplate 的某些部分:

<Border Background="{TemplateBinding Background}">
  ....
</Border>

这将允许消费者覆盖默认背景,同时保持样式定义的背景:

<your:YourControl/> <!-- Will use default Background -->

<your:YourControl Background="Green"/> <!-- will have Green background -->
于 2013-04-15T20:22:14.337 回答