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

标签 c# wpf xaml

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

这是用户控件:

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

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

最佳答案

问题是因为你的Thumb对象被引用到 Grid 的左侧和顶部,当您更改 Grid 的大小时这反过来又导致Thumb的有效相对运动。除了鼠标引起的移动之外。

更改 Thumb对齐为RightBottom ,并移动Thumb定位来自Thumb.MarginGridWidthHeight属性允许 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>

关于c# - 通过拇指调整用户控件的 wpf 大小与拖动不成比例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39194850/

相关文章:

c# - 基于对象集合的动态ContextMenu的模式/方法

wpf - 如何使用 MVVM 从 BackgroundWorker 内部更新 ObservableCollection?

c# - 将文件夹名称与 Productivity Power Tools 颜色编码相匹配的正则表达式

c# - .NETCore(Windows 8 框架)的 `GetCustomAttributes` 的等效方法是什么?

c# - 将等于和不等于传递给函数

c# - 将数据绑定(bind)/设置源到我的 DataGrid [WPF] 的正确方法是什么

c# - 如何解决 Windows Phone 8 应用程序中的以下错误?

c# - 使用 .net 4.6.2 的异步方法后 OperationContext 为 null

WPF 如何创建一个漂亮的字母波浪

silverlight - 嵌套的 silverlight 转换的执行顺序是什么?