Crop transform in WPF?

2019-07-12 19:38发布

问题:

WPF allows to use subclasses of Transform to scale(ScaleTransform), rotate(RotateTransform), skew (SkewTransform) and so on any FrameworkElement.

But I cannot see how to crop some FrameworkElement using these. Is there any way how to crop lets say a Label of width 30px so it will behave as if its width was 20px?

To be more exact: I want to do this before laying out so that the Label would be laid out as if its width was 20. But I want it to be rendered fully so the last 10 pixels will be rendered too (possibly overlapping other elements). How can I do this ?

Why do I need this ? I want to make H.B.'s answer to my question Create guitar chords editor in WPF to work with kerning.

回答1:

Use a negative Margin on the label, e.g. Margin="0,0,-10,0" makes the Label 10 pixels shorter on the right, layout-wise. (To prevent overlap put it in a container and set ClipToBounds="True")



回答2:

My problem with H.B.'s solution was, that i can't bind only the top margin.

i use XAML like this to build kindof a round progressbar (an ellipse, that gets cropped at the top):

<Canvas Name="ProgressIndicator" Width="120" Height="{Binding ProgressIndicatorHeight}" ClipToBounds="True">
    <Ellipse Width="120" Canvas.Bottom="0" Height="120" Fill="#FF7090B7"/>
</Canvas>

When i change the height of the canvas, the top of the ellipse gets cropped (because it is aligned to the bottom of the canvas). if i aligned it to the top (default), it would be cropped on the bottom.

I use this construct inside a round icon to fill it up proportionaly to the progress.