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.
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.
1 Attachment(s)
Re: Need help with data binding
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).
Re: Need help with data binding
I have since resolved this issue. I learned something new today. :)
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..
Re: Need help with data binding
What is the binding error telling you?
Re: Need help with data binding
Hi Arjay,
The binding error is as follows:
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')
Whereas my xaml looks like:
<ContentControl x:Class="Project.Views.DiagnosticsHierarchyView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="clr-namespace:Project.Controls"
x:Name="_diagnosticsHierarchyViewControl">
<Controls:AssetTreeControl x:Name="assetTreeControl"
AssetCollection="{Binding Hierarchy}"
AutomationId="HierarchyTree"
AllowDrop="False" />
</ContentControl>
HierarchyTree is a string value
Looking for a positive reply :)
Re: Need help with data binding
I mean how do you interpret the error?
Code:
BindingExpression path error: 'AutomationId' property not found on 'object' ''DiagnosticsHierarchyView' (Name='_diagnosticsHierarchyViewControl')'.
It seems to me that the error message is telling you that there isn't an AutomationId property on the DiagnosticsHierarchView object.
Does the property exist on that object? Should you be pointing to another object? Is the property public?
In other words, find out why you are getting the error.
Re: Need help with data binding
Hello
May be this code help you
Code:
//Basket class
public class Basket
{
//Private fields
private readonly BindingList<Order> _orders = new BindingList<Order>();
//Public properties
public IList<Order> Orders
{
get { return _orders; }
}
}
//Initialize the grid
public void InitializeGrid(Grid grid, IList<Basket> baskets)
{
//Bind the grid to a basket collection
grid.DataSource = baskets;
//Add orders to each basket row
foreach(Basket basket in baskets)
{
Row row = grid.DataObjects.FindFirstRow(basket);
if(row != null)
{
foreach(Order order in basket.Orders)
{
row.Add(order);
}
}
}
//Add some other data object on the topmost hierarchical level
grid.Rows.Add(new SomeOtherData());
}
you can bind one or multiple data sources with Grid.DataSource property with simultaneous adding of objects with Grid.Rows.Add() / Row.Add() methods. more here
http://www.******.com/en/net-suite/n...adding-objects