【问题标题】:WPF: add to margin without overriding existing valueWPF:添加到边距而不覆盖现有值
【发布时间】:2012-05-26 07:15:10
【问题描述】:

我定义了两种简单的边距样式,一种基于另一种。

<Style x:Key="marginStyle" TargetType="FrameworkElement">
    <Setter Property="Margin" Value="0,10,20,10"/>
</Style>

<!-- based on marginStyle -->
<Style x:Key="marginIndentStyle" TargetType="FrameworkElement" BasedOn="{StaticResource marginStyle}">
    <Setter Property="Margin" Value="10,0,0,0"/>
</Style>

在派生的 'marginIndentStyle' 样式中,我想将边距的 Left 属性调整为比基础 'marginStyle' 样式中的 Left 属性多 10,即比当前设置的值多 10。使用上面的类似内容会完全覆盖这些值。我只想添加到它,使得派生的“marginIndentStyle”样式的结果边距是“10,10,20,10”。

注意,我不想将其值严格设置为 10、10、20、10 b/c,我希望“marginStyle”样式的任何更改都反映在派生的“marginIndentStyle”样式中。

这可能吗?

【问题讨论】:

    标签: wpf xaml margin


    【解决方案1】:

    AFAIK,如果没有大量代码,这是不可能的。

    一种更简单的方法是让两种样式具有静态边距,应用于两个不同的面板\装饰器。

    类似:

    <Border Style="{StaticResource marginIndentStyle}">
        <Border Style="{StaticResource marginStyle}">
             .....
        </Border>
    </Border>
    

    这实际上会增加利润。因此,第二个边框中的任何内容都将具有作为第一个和第二个边距组合的边距。

    【讨论】:

    • 我没想到,它可能会起作用。非常感谢!标记为答案(由于我仍然是 stackoverflow 新手,所以还不能投票)
    • 没问题。请注意:很少使用此技术,因为它加深了视觉树。如果滥用过多,会影响 UI 的渲染时间。
    猜你喜欢
    • 1970-01-01
    • 2012-03-31
    • 2015-05-08
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 2015-04-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多