WPF: add to margin without overriding existing value

笑着哭i 提交于 2019-12-11 18:09:05

问题


I have two simple margin styles defined, one based off the other.

<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>

In the derived 'marginIndentStyle' style, I want adjust the margin's Left prop to be 10 more than the Left prop in the base 'marginStyle' style, that is 10 more than what it is currently set at. Using a like above overrides the values completely. I just want to add to it such that the resultant margin for the derived 'marginIndentStyle' style is "10,10,20,10".

Note, I dont want to strictly set its value to 10,10,20,10 b/c I want any changes in the 'marginStyle' style to be reflected in the derived 'marginIndentStyle' style.

Is this possible?


回答1:


AFAIK, this is not possible without a sizable amount of code.

An easier way will be to have two styles with static margins that are applied to two different panels\decorators.

Something like:

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

This, effectively, will compound the margins. So what ever is in the second border will have the margin as combination of the first and the second margins.



来源:https://stackoverflow.com/questions/10657275/wpf-add-to-margin-without-overriding-existing-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!