I am new to this, and sort of self teaching - I am trying to do the following: a) drag and drop a file into a window b) extract the icon from this file c) display filename in one window, and display the associated icon in another window.

I got the icon to extract in one program, and I have the file dragging and dropping to display filename.... NOW, I am trying to combine the two!

I am using Visual Studio 2010 Professional

Here is my code:



XAML

Code:
<Window x:Class="DragNDropFileIcon.MainWindow"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="MainWindow" Height="300" Width="500" BorderThickness="1">

<Window.Resources>

<ItemsPanelTemplate x:Key="ItemsPanelTemplate1">

<WrapPanel IsItemsHost="True"/>

</ItemsPanelTemplate>

<Style x:Key="ListBoxItemStyle" TargetType="{x:Type ListBoxItem}">

<Setter Property="Background" Value="Transparent"/>

<Setter Property="Padding" Value="2,0,0,0"/>

<Setter Property="Template">

<Setter.Value>

<ControlTemplate TargetType="{x:Type ListBoxItem}">

<Border HorizontalAlignment="Center" VerticalAlignment="Center">

<StackPanel Orientation="Vertical">

<Image x:Name="img" Source="{Binding FileIcon}" Height="32" Width="32"/>

<TextBlock VerticalAlignment="Center" Width="75" TextWrapping="Wrap" Text="{Binding FileName}"/>

</StackPanel>

</Border>

</ControlTemplate>

</Setter.Value>

</Setter>

</Style>

</Window.Resources>

<Grid Background="BurlyWood" >

<ListBox x:Name="FileIconArea" 

Width="105"

Background="AntiqueWhite" 

BorderBrush="Bisque" 

BorderThickness="2"

HorizontalAlignment="Left" 

Panel.ZIndex="1" 

Margin="5,0,0,0"

ItemsSource="{Binding ElementName=FileIcon, Path=MyFiles}"

 

ItemContainerStyle="{DynamicResource ListBoxItemStyle}"

ItemsPanel="{DynamicResource ItemsPanelTemplate1}"

ScrollViewer.VerticalScrollBarVisibility="Visible" DataContext="{Binding FileIcon}" IsSynchronizedWithCurrentItem="True">

 

<TextBlock x:Name="txtFileIcon" 

Text="File Icon"

FontFamily="Lucida Sans" 

FontWeight="Bold" 

FontStyle="Italic" 

FontSize="14"

Width="141"

TextAlignment="Center" 

VerticalAlignment="Center" DataContext="{Binding ElementName=DropArea, Path=ItemsSource/}" />

 

</ListBox> 

 

<ListBox x:Name="DropArea" 

Width="360"

Background="Ivory" 

BorderBrush="Bisque" 

BorderThickness="2"

AllowDrop="True" 

Drop="DropArea_Drop"

HorizontalAlignment="Right"

ScrollViewer.VerticalScrollBarVisibility="Visible">

<TextBlock x:Name="txtFileName" 

Text="File Name"

FontFamily="Lucida Sans" 

FontWeight="Bold" 

FontStyle="Italic" 

FontSize="14"

Width="324"

TextAlignment="Center" 

VerticalAlignment="Center" />

</ListBox> 

</Grid>

</Window>

codebehind


Code:
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Navigation;

using System.Windows.Shapes;

namespace DragNDropFileIcon

{

/// <summary>

/// Interaction logic for MainWindow.xaml

/// </summary>

public partial class MainWindow : Window

{

public MainWindow()

{

InitializeComponent();

}

#region Drop

private void DropArea_Drop(object sender, DragEventArgs e)

{

if (e.Data.GetDataPresent(DataFormats.FileDrop))

{

//DropListBox.Items.Clear();

string[] droppedFilePaths = e.Data.GetData(DataFormats.FileDrop, true) as string[];

foreach (string droppedFilePath in droppedFilePaths)

{

 

ListBoxItem fileItem = new ListBoxItem();

fileItem.Content = System.IO.Path.GetFileNameWithoutExtension(droppedFilePath);

fileItem.ToolTip = droppedFilePath;

// if (fileItem.DataContextChanged 

// {

// }

DropArea.Items.Add(fileItem);

 

}

}

}

#endregion

#region ListBoxSearch

private void ListBoxSearch(bool sender, RoutedEventArgs e)

{

foreach (ListBoxItem li in DropArea.Items)

{

ListBoxItem fileItem = new ListBoxItem();

if (li.ToolTip == fileItem.ToolTip)

{

MessageBox.Show("It exists!");

break;

}

 

DropArea.Items.Add(fileItem);

}

}

#endregion

#region MyFiles

public class MyFiles

{

public string FileName { get; set; }

public ImageSource FileIcon { get; set; }

}

#endregion

#region FileToImageIconConverter

public class FileToImageIconConverter

{

private string filePath;

private System.Windows.Media.ImageSource icon;

public string FilePath { get { return filePath; } }

public System.Windows.Media.ImageSource Icon

{

get

{

if (icon == null && System.IO.File.Exists(FilePath))

{

using (System.Drawing.Icon sysicon = System.Drawing.Icon.ExtractAssociatedIcon(FilePath))

{

icon = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(

sysicon.Handle,

System.Windows.Int32Rect.Empty,

System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());

}

}

return icon;

}

}

public FileToImageIconConverter(string filePath)

{

this.filePath = filePath;

}

}

#endregion

}

}


I am pretty sure that it is a data binding issue, and my lack of knowledge, if anyone can help me I would appreciate it.