CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Hybrid View

  1. #1
    Join Date
    Aug 2011
    Posts
    3

    Question Need help with data binding

    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.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Need help with data binding

    Say, can you zip up and post a small sample project?

    It would make it easier for folks to just load up the project and check things out, rather than trying to spot the error looking through the code posted here.

  3. #3
    Join Date
    Aug 2011
    Posts
    3

    Re: Need help with data binding

    Yes I can thanks.
    Attached Files Attached Files

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Need help with data binding

    Binding errors will show up in the Output window while you are debugging the program from within the IDE.

    System.Windows.Data Error: 40 : BindingExpression path error: 'FileIcon' property not found on 'object' ''TextBlock' (Name='txtFileIcon')'. BindingExpression:Path=FileIcon; DataItem='TextBlock' (Name='txtFileIcon'); target element is 'Image' (Name=''); target property is 'Source' (type 'ImageSource')
    System.Windows.Data Error: 40 : BindingExpression path error: 'FileName' property not found on 'object' ''TextBlock' (Name='txtFileIcon')'. BindingExpression:Path=FileName; DataItem='TextBlock' (Name='txtFileIcon'); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
    System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=FileIcon'. BindingExpression:Path=MyFiles; DataItem=null; target element is 'ListBox' (Name='FileIconArea'); target property is 'ItemsSource' (type 'IEnumerable')
    The thread '<No Name>' (0xf54) has exited with code 0 (0x0).

  5. #5
    Join Date
    Aug 2011
    Posts
    3

    Re: Need help with data binding

    I have since resolved this issue. I learned something new today.

  6. #6
    Join Date
    Feb 2012
    Posts
    2

    Re: Need help with data binding

    Hi lynnosler, how did you fix that ? I am too facing the same problem.

    System.Windows.Data Error: 40 : BindingExpression path error: 'AutomationId' property not found on 'object' ''DiagnosticsHierarchyView' (Name='_diagnosticsHierarchyViewControl')'. BindingExpression:Path=AutomationId; DataItem='DiagnosticsHierarchyView' (Name='_diagnosticsHierarchyViewControl'); target element is 'TabItem' (Name=''); target property is 'AutomationId' (type 'String')

    Waiting for your reply..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured