I have a listview and also a DataGrid having records with images. My problem is that when I try to add the record in the listview or in the Datagrid I can only add the string or numbers only but can’t add an image. I tried to find it through an openfiledialog box and try to assign to the datagrid cell but i don’t know to what to assign the Image at run time if i search for the image. Another problem is that i have been binding the image of the Datagrid but i try to update it and add it at runtime .
And my second problem is updating the image. If any one know about the adding and updating the record in the listview and the also the Datagrid using template so please please for for the sack of God help me i try it but cant fix it . If any one who is expert of wpf please tell me your few minutes can solve my problem please.
My .xmal file is
<Window x:Class="UI.ViewClasses"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:UI"
Title="ViewClasses" Height="300" Width="456" xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit" Loaded="Window_Loaded">
<Window.Resources >
<local:ImageConverter x:Key="ImageDataConverter"/>
</Window.Resources>
<Grid>
<my:DataGrid ItemsSource="{Binding}" Name="DataGrid" AutoGenerateColumns="False" Margin="12,51,80,35" SelectionMode="Extended" SelectionUnit="Cell" CanUserReorderColumns="True" CanUserResizeColumns="True"
CanUserResizeRows="False" CanUserSortColumns="True" IsReadOnly="False" LoadingRow="DataGrid_LoadingRow" AlternatingRowBackground="LightBlue"
Loaded="DataGrid_Loaded">
<my:DataGrid.Columns>
<my:DataGridTemplateColumn Header=" Frist Name">
<my:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=FirstName}"
Margin="-6,0,-6,0"/>
</DataTemplate>
</my:DataGridTemplateColumn.CellTemplate>
</my:DataGridTemplateColumn>
<my:DataGridTemplateColumn Header=" Last Name">
<my:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=LastName}"
Margin="-6,0,-6,0"/>
</DataTemplate>
</my:DataGridTemplateColumn.CellTemplate>
</my:DataGridTemplateColumn>
<my:DataGridTemplateColumn Header=" Gender">
<my:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=Gender}"
Margin="-6,0,-6,0"/>
</DataTemplate>
</my:DataGridTemplateColumn.CellTemplate>
</my:DataGridTemplateColumn>
<my:DataGridTemplateColumn Header=" GPA">
<my:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=GPA}"
Margin="-6,0,-6,0"/>
</DataTemplate>
</my:DataGridTemplateColumn.CellTemplate>
</my:DataGridTemplateColumn>
<my:DataGridTemplateColumn Header=" Image">
<my:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Height="50" Name="image1" Source="{Binding Path=MyImage, Converter={StaticResource ImageDataConverter}}" />
</DataTemplate>
</my:DataGridTemplateColumn.CellTemplate>
</my:DataGridTemplateColumn>
</my:DataGrid.Columns>
</my:DataGrid>
<Button Height="30" Margin="12,10,0,0" Name="btnAdd" VerticalAlignment="Top" HorizontalAlignment="Left" Width="75" Click="btnAdd_Click">Add</Button>
<Button Height="30" Margin="166,10,0,0" Name="btnSave" VerticalAlignment="Top" Click="btnSave_Click" HorizontalAlignment="Left" Width="75">Save</Button>
<Button Height="30" HorizontalAlignment="Left" Margin="92,10,0,0" VerticalAlignment="Top" Width="75" Name="btnDelete" Click="btnDelete_Click">Delete</Button>
<Button Height="30" HorizontalAlignment="Right" Margin="0,10,112,0" Name="Browses" VerticalAlignment="Top" Width="83" Click="Browses_Click">Brows</Button>
<TextBox Height="30" HorizontalAlignment="Right" Margin="0,10,0,0" Name="txtBrowseFile" VerticalAlignment="Top" Width="110" />
</Grid>
</Window>
And my .xaml.cs file is
namespace UI
{
/// <summary>
/// Interaction logic for ViewClasses.xaml
/// </summary>
public partial class ViewClasses : Window
{
public ViewClasses()
{
InitializeComponent();
} private DataClasses1DataContext db = new DataClasses1DataContext();
private BindingListCollectionView CustomerView;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
DataClasses1DataContext db = new DataClasses1DataContext();
var custsInCA = from c in db.Students
select c;
this.DataContext = custsInCA;
this.CustomerView = ((BindingListCollectionView)(CollectionViewSource.GetDefaultView(this.DataContext)));
}
private void DataGrid_LoadingRow(object sender, Microsoft.Windows.Controls.DataGridRowEventArgs e)
{
}
private void DataGrid_Loaded(object sender, RoutedEventArgs e)
{
}
private void btnSave_Click(object sender, RoutedEventArgs e)
{
try
{
this.db.SubmitChanges();
MessageBox.Show("Saved");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
Student st = ((Student)(this.CustomerView.AddNew()));
st.LastName = "<new>";
this.CustomerView.CommitNew();
this.DataGrid.ScrollIntoView(st);
}
private void btnDelete_Click(object sender, RoutedEventArgs e)
{
if ((this.CustomerView.CurrentPosition > -1))
{
this.CustomerView.RemoveAt(this.CustomerView.CurrentPosition);
}
}
private void Browses_Click(object sender, RoutedEventArgs e)
{
Image image1 = new Image();
Microsoft.Windows.Controls.DataGrid df = new Microsoft.Windows.Controls.DataGrid();
Microsoft.Win32.OpenFileDialog fileChooser = new Microsoft.Win32.OpenFileDialog();
fileChooser.Filter = " Image files|*.jpg;*.gif;*.bmp;*.png;;*.jpeg";
Nullable<bool> result = fileChooser.ShowDialog();
if (result == true)
try
{
txtBrowseFile.Text = fileChooser.FileName;
}
catch { return; }
if (txtBrowseFile.Text.Trim().Length != 0)
{
BitmapImage src = new BitmapImage();
src.BeginInit();
src.UriSource = new Uri(txtBrowseFile.Text.Trim(), UriKind.Relative);
src.CacheOption = BitmapCacheOption.OnLoad;
src.EndInit();
image1.Source = src;
}
}
}
public class ImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
System.Data.Linq.Binary binaryData = value as System.Data.Linq.Binary;
//System.Data.Linq.Binary binaryData = value;// here there is the first error .How convert BinaryData to Object??
if (binaryData == null)
{
return null;
}
byte[] buffer = binaryData.ToArray();
if (buffer.Length == 0)
{
return null;
}
BitmapImage res = new BitmapImage();
res.BeginInit();
res.StreamSource = new System.IO.MemoryStream(buffer);
res.EndInit();
return res;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
My problem is that I want to add the record at runtime I can add the other record but cant image and also can’t update the image at run time in the datagrid. If there is any expert in the wpf then please replay my and replay me if possible in full detail that I can understand I have electric load shading problem in my country so I can’t quick respond to you so please if possible respond me for the sack of God I just wishing the noble persons those how helping me .
I have previously posted an answer with some info about INotifyPropertyChanged.
how to add data to database and show it in datagrid without restarting program?
Let me know if that is satisfacctory to your purpose of if you require additional help.
EDIT
One thing I would do is change the way you are working with your items.
I generally use something more like this:
And in XAML:
In this way, your DataGrids ItemsSource is bound directly to the CollectionViewSource, the CollectionViewSource is fed by the Students collection, and the change notification of change is sent up when the Students collection has been updated.
The above code is just test code, there may be some spelling or punctuation mistakes. I am unfortunatly on the bus right now and not at my desk.
Also, are you using Entity Framework?