wpf通过拇指调整用户控件的大小与拖动不成比例

时间:2016-08-28 19:09:33

标签: c# wpf xaml

我正在尝试通过位于usercontrol内部的拇指来调整自定义用户控件的大小。

这是usercontrol:

<UserControl x:Class="ER.Entity"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:ER"
         mc:Ignorable="d"
         Name="entityRoot">
<Grid Name="entityGrid">
    <Thumb x:Name="resizeThumb" Height="10" Width="10" Margin="200,90,-15,-10" DragDelta="Resize" />
    <Border  Opacity="100" BorderThickness="5" BorderBrush="Black">
        <TextBlock x:Name="textBox1" TextWrapping="Wrap" Text="{Binding ElementName=entityRoot, Path=EntityName}" VerticalAlignment="Top" PreviewMouseDown="EntityPreviewMouseDownHandler" PreviewMouseMove="EntityPreviewMouseMoveHandler" PreviewMouseUp="EntityPreviewMouseUpHandler" />
    </Border>
</Grid>

这是DragDelta事件触发的方法:

private void Resize(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
    {
        entityGrid.SetValue(WidthProperty, entityGrid.ActualWidth + e.HorizontalChange);
        entityGrid.SetValue(HeightProperty, entityGrid.ActualHeight + e.VerticalChange);
    }

问题在于,当我拖动拇指时,用户控件的大小调整比鼠标移动大得多。

1 个答案:

答案 0 :(得分:1)

问题在于,因为Thumb对象被引用到Grid的左侧和顶部,当您更改Grid的大小时,会导致有效的相对移动Thumb以及鼠标引起的移动。

Thumb路线更改为RightBottom,并将Thumb定位从Thumb.Margin移至Grid&# 39; s WidthHeight属性允许Thumb按预期调整Grid的大小:

<Grid Name="entityGrid" Width="200" Height="90">
  <Thumb x:Name="resizeThumb" Height="10" Width="10"
         HorizontalAlignment="Right" VerticalAlignment="Bottom"
         Margin="0,0,-15,-10" DragDelta="Resize" />
  <Border  Opacity="100" BorderThickness="2" BorderBrush="Black">
    <TextBlock x:Name="textBox1" TextWrapping="Wrap"
               Text="{Binding ElementName=entityRoot, Path=EntityName}"
               VerticalAlignment="Top"
               PreviewMouseDown="EntityPreviewMouseDownHandler"
               PreviewMouseMove="EntityPreviewMouseMoveHandler" 
               PreviewMouseUp="EntityPreviewMouseUpHandler" />
  </Border>
</Grid>