拖动文本并在鼠标悬停将其拖放到TextBox时,请增加TextBox的大小并在其他控件上重叠

时间:2019-07-19 21:01:08

标签: c# wpf

每当用户从Treeview控件中拖动节点并将鼠标悬停在TextBox上时,我都希望增加TextBox控件的大小。 增大尺寸不应重新调整其他控件,而当前控件应与相邻控件重叠。

我尝试实现代码WPF: On Mouse hover on a particular control, increase its size and overlap on the other controls

,但是当将鼠标悬停在TextBox上并按下鼠标左键拖动文本时,该功能将无效。

<ItemsControl Margin="50">
        <ItemsControl.Resources>
            <Style x:Key="ScaleStyle" TargetType="TextBox">
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Grid.ZIndex" Value="1"/>
                        <Setter Property="RenderTransform">
                            <Setter.Value>
                                <ScaleTransform ScaleX="1.1" ScaleY="1.1"/>
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </ItemsControl.Resources>
</ItemsControl>

2 个答案:

答案 0 :(得分:0)

从另一个角度进行处理。使用后面的代码处理左键单击和拖动。

伪代码...     如果将鼠标悬停在textbox.text == true

Textbox size = 300;

然后检查文本框的网格位置。应该允许它在其他列上跨列,而其余控件在其grid.row和grid.column位置保持固定。

答案 1 :(得分:0)

这是一个小示例应用程序。与我的评论相反,我们需要PreviewDragEnter事件,因为文本框已具有“拖放”支持。在Window_Loaded中,应用程序注册事件处理程序。然后,在TextBox_PreviewDragEnter中,手动设置新样式。我们还存储了旧的z-index,以允许将其还原到TextBox_PreviewDragLeave中。

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Height="350" Width="525" Loaded="Window_Loaded">
    <StackPanel Margin="8">
        <TextBox/>
        <TextBox/>
        <TextBox/>
        <TextBox/>
    </StackPanel>
</Window>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    //From https://stackoverflow.com/a/978352/1210053
    public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
    {
        if (depObj != null)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                if (child != null && child is T)
                {
                    yield return (T)child;
                }

                foreach (T childOfChild in FindVisualChildren<T>(child))
                {
                    yield return childOfChild;
                }
            }
        }
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        foreach (var txt in FindVisualChildren<TextBox>(this))
        {
            txt.PreviewDragEnter += TextBox_PreviewDragEnter;
            txt.PreviewDragLeave += TextBox_PreviewDragLeave;
            txt.PreviewDrop += TextBox_PreviewDragLeave;
        }

    }

    private Dictionary<TextBox, int> oldZIndex = new Dictionary<TextBox, int>();

    private void TextBox_PreviewDragEnter(object sender, DragEventArgs e)
    {
        var txt = (TextBox)sender;
        oldZIndex.Add(txt, Panel.GetZIndex(txt));
        Panel.SetZIndex(txt, 1);
        var scaleTransform = new ScaleTransform(1.1, 1.1, txt.ActualWidth / 2, txt.ActualHeight / 2);
        txt.RenderTransform = scaleTransform;
    }

    private void TextBox_PreviewDragLeave(object sender, DragEventArgs e)
    {
        var txt = (TextBox)sender;
        txt.RenderTransform = null;
        Panel.SetZIndex(txt, oldZIndex[txt]);
        oldZIndex.Remove(txt);
    }             
}