简体   繁体   中英

Resizing only a clipped area of an User Control in WPF

I have a predefined User Control from a third party library. I would like to cut off a region from this User Control and use it in the Main window. Clipping is easily done with the Clip property of a UIElement but then the resizing does not work for this single segment. Is it possible to cut from a UIElement or User Control and use only this region for measuring, resizing and rendering of the component?

Please see a sample below :

XAML

<Window x:Class="WpfStackOverflow.Window12"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfStackOverflow"
        Title="Window12" Height="300" Width="600">    

    <Canvas>
        <Button Content="Press" Height="35" Width="75" Canvas.Left="29" Canvas.Top="43" Click="Button_Click_1">                
            <Button.Background>
                <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                    <GradientStop Color="#FF62CFB6" Offset="0"/>
                    <GradientStop Color="#FFEBEBEB" Offset="0.5"/>
                    <GradientStop Color="#FF91B3E4" Offset="0.391"/>
                    <GradientStop Color="#FFF11278" Offset="1"/>
                </LinearGradientBrush>
            </Button.Background>
            <Button.Clip>
                <GeometryGroup>
                    <EllipseGeometry RadiusX="50" RadiusY="25"/>
                    <RectangleGeometry Rect="55 20 75 35"/>
                </GeometryGroup>
            </Button.Clip>
        </Button>

        <Path x:Name="Pth" Width="75" Height="35" Fill="AliceBlue" Canvas.Top="80" Canvas.Left="300" Stretch="None" Stroke="#FF7C4444" Data=""/>

        <Thumb Width="10" Height="10" Canvas.Left="217" Canvas.Top="34" DragDelta="Thumb_DragDelta_1"/>
        <TextBlock Canvas.Left="217" TextWrapping="Wrap" Text="Drag this Thumb to right" Canvas.Top="10"/>
        <TextBlock Canvas.Left="10" TextWrapping="Wrap" Text="Click this button" Canvas.Top="10"/>
    </Canvas>

</Window>

CODE

using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;

namespace WpfStackOverflow
{
    /// <summary>
    /// Interaction logic for Window12.xaml
    /// </summary>
    public partial class Window12 : Window
    {
        public Window12()
        {
            InitializeComponent();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            var c = VisualTreeHelper.GetClip(sender as Button);
            Pth.Data = c;
        }

        double scaleX=1, scaleY=1;
        private void Thumb_DragDelta_1(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
        {
            if (Pth.LayoutTransform == null)
                Pth.LayoutTransform = new ScaleTransform(1, 1);

            scaleX += e.HorizontalChange;
            scaleY += e.HorizontalChange;

            Pth.LayoutTransform = new ScaleTransform(scaleX, scaleY);

            System.Diagnostics.Debug.WriteLine((1 + e.HorizontalChange).ToString());
            Thickness margin = (sender as Thumb).Margin;
            (sender as Thumb).Margin = new Thickness(margin.Left + e.HorizontalChange, margin.Top, margin.Right, margin.Bottom);
        }
    }
}

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