【问题标题】:WPF ListViewItem Drag and Drop onto TextBlockWPF ListViewItem 拖放到 TextBlock
【发布时间】:2018-04-03 06:57:13
【问题描述】:

我正在 WPF 中编写一个简单的地址簿示例应用程序,其中我有一个 listview 将联系人保存为用户对象,但每个项目都用一个仅显示名称的 TextBlock 表示。

我想要实现的是用户可以将项目拖到“组”上。组显示在窗口的左侧。它是一个简单的网格列,包含多个 TextBlock 项目。

我的相关代码隐藏:

    /** Display Contact in the contact pane**/
    private void lstItemContact_MouseDown(object sender, MouseButtonEventArgs e)
    {

        ListViewItem item = (ListViewItem)sender;

        User selectedUser = (User) item.Content;

        DragDrop.DoDragDrop(lstContacts, item, DragDropEffects.Move);

        contactPane.Fullname = selectedUser.Name;
        contactPane.Email = selectedUser.Mail;
    }

    private void tbFavouritesDrop_Drop(object sender, DragEventArgs e)
    {
        User dropped_user = (User)sender;

        MessageBox.Show(dropped_user.Name);
    }

还有 XAML:

<ListView x:Name="lstContacts" Width="250" Grid.Row="1" Grid.Column="1">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" />
            </GridView>
        </ListView.View>

        <ListView.GroupStyle>
            <GroupStyle>
                <GroupStyle.HeaderTemplate>
                    <DataTemplate>
                        <TextBlock x:Name="lstItemContact" FontWeight="Bold" FontSize="14" Text="{Binding Name}" />
                    </DataTemplate>
                </GroupStyle.HeaderTemplate>
            </GroupStyle>
        </ListView.GroupStyle>
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <EventSetter Event="PreviewMouseLeftButtonDown" Handler="lstItemContact_MouseDown" />
            </Style>                
        </ListView.ItemContainerStyle>
    </ListView>



<TextBlock AllowDrop="True" Drop="tbFavouritesDrop_Drop" Name="tbFavouritesDrop">
                    <Border CornerRadius="3" Background="OrangeRed" Height="7" Width="7" VerticalAlignment="Center" Margin="0 0 2 5"/>
                    <Label FontFamily="Segoe UI Light" FontSize="14" Padding="0">Favourites</Label>
                </TextBlock>

将项目拖到“收藏夹”组后,我收到以下错误: 附加信息:无法将“System.Windows.Controls.TextBlock”类型的对象转换为“AddressBook1.User”类型

我不确定确切的问题是什么?问题是我的列表视图项目是文本块吗?我发送的项目是一个用户对象,它也到达tbFavouritesDrop_Drop

编辑 1:

我已将 Drop 事件处理程序更改为:

private void tbFavouritesDrop_Drop(object sender, DragEventArgs e)
        {
            TextBlock item = (TextBlock)sender;

            MessageBox.Show(item.Text);
        }

没有抛出异常,但是.Text 属性为空。

【问题讨论】:

  • 我的怀疑是,如果您设置断点并单步执行,异常实际上发生在它似乎发生的位置上方的一行。 (堆栈跟踪会告诉你是否曾将其加入 DoDragDrop 方法)

标签: c# wpf xaml drag-and-drop


【解决方案1】:

好的,我在另一个帖子发疯后找到了解决方案:WPF Drag and Drop - Get original source info from DragEventArgs

剩下的就是:

/** Display Contact in the contact pane**/
    private void lstItemContact_MouseDown(object sender, MouseButtonEventArgs e)
    {

        ListViewItem item = (ListViewItem)sender;

        User selectedUser = (User) item.Content;

        contactPane.Fullname = selectedUser.Name;
        contactPane.Email = selectedUser.Mail;
    }

    private void lstItemContact_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.LeftButton == MouseButtonState.Pressed)
        {
            if (e.Source != null)
            {
                User selectedItem = (User) lstContacts.SelectedItem;

                DragDrop.DoDragDrop(lstContacts, selectedItem, DragDropEffects.Move);
            }
        }
    }

    private void tbFavouritesDrop_Drop(object sender, DragEventArgs e)
    {
        User selectedUser = e.Data.GetData("AddressBook1.User") as User;
        MessageBox.Show(selectedUser.Name);

    }

我希望这对其他人有所帮助。这里的重点是使用e.Data.GetData("AddressBook1.User"),而不是与发件人一起工作。

【讨论】:

    猜你喜欢
    • 2012-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-30
    相关资源
    最近更新 更多