将项添加到ListBox后,DragDrop中的NullReferenceException

时间:2012-06-27 14:56:46

标签: c# wpf drag-and-drop listbox nullreferenceexception

我正在尝试实现一个包含程序“打开文件”的动态ListBox。这些文件可以从ListBox拖动到四个画布中的一个。 只要在启动程序之前在XAML中添加项目就可以正常工作,但是,一旦我通过fileList.Items.Add(“myitemname”)将项目添加到ListBox;如果我尝试将它们(拖动工作)放入Canvas at

,我会得到一个NullReferenceException
DragDrop.DoDragDrop(listBox, dragData, DragDropEffects.Move);

这是我的代码的相关部分:

public partial class MainWindow : Window
{
    InitialDataObject _initData = new InitialDataObject();

    public MainWindow()
    {
        InitializeComponent();
    }


    #region DragImage

    private void DragImageStart(object sender, MouseButtonEventArgs e)
    {
        _initData._mousePoint = e.GetPosition(null);
    }

    private void DragImageMove(object sender, MouseEventArgs e)
    {
        Point mousePos = e.GetPosition(null);
        Vector diff = _initData._mousePoint - mousePos;

        if (e.LeftButton == MouseButtonState.Pressed && (
        Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
        Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance) && ((sender as ListBox).SelectedItem != null))
        {
            var listBox = sender as ListBox;
            var listBoxItem = listBox.SelectedItem;
            DataObject dragData = new DataObject(_initData._dropIdentifier, listBoxItem);
            DragDrop.DoDragDrop(listBox, dragData, DragDropEffects.Move); 
        }

    }

    private void CanvasDrop(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(_initData._dropIdentifier))
        {
            var item = e.Data.GetData(_initData._dropIdentifier) as ListBoxItem;
            (sender as Canvas).Background = new SolidColorBrush(Color.FromArgb(255, 255, 255, 255));
            DropImage(sender as Canvas, item);
            fileList.UnselectAll();
        }
    }

    private void CanvasDragEnter(object sender, DragEventArgs e)
    {
        if (!e.Data.GetDataPresent(_initData._dropIdentifier) || sender == e.Source)
        {
            (sender as Canvas).Background = new SolidColorBrush(Color.FromArgb(255, 240, 240, 240));
            e.Effects = DragDropEffects.None;
        } 
    }

    private void DropImage(Canvas targetCanvas, ListBoxItem item)
    {
        //just to check if I got the right item in this method
        MessageBox.Show(item.Content.ToString());
    }

    private void CanvasDragLeave(object sender, DragEventArgs e)
    {
        if (!e.Data.GetDataPresent(_initData._dropIdentifier) || sender == e.Source)
        {
            (sender as Canvas).Background = new SolidColorBrush(Color.FromArgb(255, 255, 255, 255));
        }
    }

    #endregion

    private void sdfsdf(object sender, RoutedEventArgs e)
    {
        fileList.Items.Add("test");
    }
}

class InitialDataObject
{
    public Point _mousePoint = new Point();
    public readonly string _dropIdentifier = "dropIdentifier";
}

XAML:

<Grid Height="Auto" HorizontalAlignment="Stretch" Margin="0,23,0,0" Name="gridSubmain" VerticalAlignment="Stretch" Width="Auto" Panel.ZIndex="2">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="250" MaxWidth="250" MinWidth="250" />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <ListBox Height="Auto" Name="fileList" Width="Auto" Background="#FFE6E6E6" BorderBrush="{x:Null}" Panel.ZIndex="1" PreviewMouseLeftButtonDown="DragImageStart" PreviewMouseMove="DragImageMove" FontSize="16" ItemsSource="{Binding}" Margin="0" Grid.Row="2">
        <ListBoxItem Content="dfgdfg" />
        <ListBoxItem Content="sfsdf" />
        <ListBoxItem Content="ghjgh" />
        <ListBoxItem Content="cvbcvb" />
    </ListBox>
    <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="112,196,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="sdfsdf" />
</Grid>
<Grid Grid.Column="1" Height="Auto" HorizontalAlignment="Stretch" Margin="0" Name="gridImage" VerticalAlignment="Stretch" Width="Auto">
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
    <Canvas Height="Auto" HorizontalAlignment="Stretch" Margin="0" Name="canvasImage1" VerticalAlignment="Stretch" Width="Auto" AllowDrop="True" Drop="CanvasDrop" DragEnter="CanvasDragEnter" Background="White" DragLeave="CanvasDragLeave" />
    <Canvas Height="Auto" HorizontalAlignment="Stretch" Margin="0" Name="canvasImage2" VerticalAlignment="Stretch" Width="Auto" Drop="CanvasDrop" DragEnter="CanvasDragEnter" Grid.Column="1" AllowDrop="True" Background="White" DragLeave="CanvasDragLeave"/>
    <Canvas Height="Auto" HorizontalAlignment="Stretch" Margin="0" Name="canvasImage3" VerticalAlignment="Stretch" Width="Auto" Drop="CanvasDrop" DragEnter="CanvasDragEnter" Grid.Row="1" AllowDrop="True" Background="White" DragLeave="CanvasDragLeave"/>
    <Canvas Height="Auto" HorizontalAlignment="Stretch" Margin="0" Name="canvasImage4" VerticalAlignment="Stretch" Width="Auto" Drop="CanvasDrop" DragEnter="CanvasDragEnter" Grid.Column="1" Grid.Row="1" AllowDrop="True" Background="White" DragLeave="CanvasDragLeave"/>
</Grid>

任何想法为什么它使用现有项目而不是通过fileList.Items.Add(“..”)添加的项目;?另外,使用现有Items的fileList.UnselectAll();工作正常,但额外添加的项目保持选择,我无法摆脱选择。

1 个答案:

答案 0 :(得分:1)

CanvasDrop方法存在问题。你期望ListBoxItem在那里,但是得到字符串,因为列表框的SelectedItem属性对你在xaml中创建的项和你动态添加的项有不同的值。