c# - wpf拖放可视化选择项目

标签 c# wpf select drag-and-drop

我在我的程序中使用拖放功能,效果很好。但是我正在处理列表框中的单词列表,当我选择一个单词并将其拖到另一个列表框时,用户不再知道他选择了哪个单词,因为第一个列表框中项目的“视觉”选择没有出现。有谁知道如何在列表框中查看所选项目?在我实现拖放之前,当我选择单词时,所选单词有另一种颜色,但是当我添加拖放时,我再也看不到它了。谁能帮帮我?

http://img196.imageshack.us/img196/8408/imgmt.jpg

    private void lstAlleTabellen_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        startpoint = e.GetPosition(null);
    }

    private void lstAlleTabellen_MouseMove(object sender, MouseEventArgs e)
    {
        // Get the current mouse position
        System.Windows.Point mousePos = e.GetPosition(null);
        Vector diff = startpoint - mousePos;

        if (e.LeftButton == MouseButtonState.Pressed &&
            Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
            Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance)
        {
            // Get the dragged ListViewItem
            System.Windows.Controls.ListBox listAlle = sender as System.Windows.Controls.ListBox;
            ListBoxItem listItem =
                FindAnchestor<ListBoxItem>((DependencyObject)e.OriginalSource);
            Tabel tabel=new Tabel();
            try
            {
                // Find the data behind the ListViewItem
                tabel = (Tabel)listAlle.ItemContainerGenerator.
                    ItemFromContainer(listItem);


                // Initialize the drag & drop operation
                DataObject dragData = new DataObject("myFormat", tabel);
                DragDrop.DoDragDrop(listItem, dragData, DragDropEffects.Move);
            }
            catch
            {
            }
        }
    }

    private static T FindAnchestor<T>(DependencyObject current) where T : DependencyObject
    {
        do
        {
            if (current is T)
            {
                return (T)current;
            }
            current = VisualTreeHelper.GetParent(current);
        }
        while (current != null);
        return null;
    }
    private void lstGekozenTabellen_DragEnter(object sender, DragEventArgs e)
    {
        if (!e.Data.GetDataPresent("myFormat") ||sender == e.Source)
        {
            e.Effects = DragDropEffects.None;
        }
    }

    private void lstGekozenTabellen_Drop(object sender, DragEventArgs e)
    {
        try
        {
            if (e.Data.GetDataPresent("myFormat"))
            {
                Tabel tabel = e.Data.GetData("myFormat") as Tabel;
                System.Windows.Controls.ListBox listGekozen = sender as System.Windows.Controls.ListBox;
                listGekozen.DisplayMemberPath = "naam";
                listGekozen.SelectedValuePath = "naam";
                listGekozen.Items.Add(tabel);
                lTabellen.Remove(tabel);

                lstAlleTabellen.ItemsSource = null;
                lstAlleTabellen.Items.Clear();
                lstAlleTabellen.ItemsSource = lTabellen;
            }


        }
        catch { }
    }

}

最佳答案

例如,您可以为此使用样式触发器(完整解决方案 here):

    <Style x:Key="ListItemStyle" TargetType="ListViewItem">
        <Style.Resources>
            <LinearGradientBrush x:Key="MouseOverBrush" StartPoint="0.5, 0" EndPoint="0.5, 1">
                <GradientStop Color="#22000000" Offset="0" />
                <GradientStop Color="#44000000" Offset="0.4" />
                <GradientStop Color="#55000000" Offset="0.6" />
                <GradientStop Color="#33000000" Offset="0.9" />
                <GradientStop Color="#22000000" Offset="1" />
            </LinearGradientBrush>
        </Style.Resources>
        <Setter Property="Width" Value="Auto" />
        <Setter Property="Padding" Value="0,4" />
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        <Setter Property="Border.BorderThickness" Value="0,0,0,0.5" />
        <Setter Property="Border.BorderBrush" Value="LightGray" />
        <Style.Triggers>
            <Trigger Property="jas:ListViewItemDragState.IsBeingDragged" Value="True">
                <Setter Property="FontWeight" Value="DemiBold" />
            </Trigger>
            <Trigger Property="jas:ListViewItemDragState.IsUnderDragCursor" Value="True">
                <Setter Property="Background" Value="{StaticResource MouseOverBrush}" />
            </Trigger>
        </Style.Triggers>
    </Style>

关于c# - wpf拖放可视化选择项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6135661/

相关文章:

javascript - 根据不同的选择框获取数组值

java - 在 VisualVm 中按对象参数对 OQL 结果集进行排序

c# - 在 RowCommand 的嵌套 GridView 中从 DropDownList 中检索值

c# - 比较两个字符串,忽略 C# 中的大小写

c# - WPF中的水平可折叠面板

c# - 是否有任何默认策略来为决议变更准备申请?

c# - 使用 CultureInfo 发出警告的代码

c# - 如何动态创建xps文件?

wpf - 将样式的 TargetType 属性设置为基类

mysql - 我如何根据 mySQL 中的另一列选择一列?