简体   繁体   中英

WPF: add to margin without overriding existing value

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM