仅调整WPF中用户控件的剪切区域的大小

时间:2016-10-20 14:08:35

标签: c# wpf xaml geometry

我有来自第三方库的预定义用户控件。我想从此用户控件中删除一个区域,并在主窗口中使用它。使用UIElement的Clip属性可以轻松完成剪切,但调整大小不适用于此单个段。是否可以从UIElement或用户控件切割并仅使用此区域来测量,调整大小和渲染组件?

1 个答案:

答案 0 :(得分:0)

请参阅以下示例:

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);
        }
    }
}