Wpf 如何在运行时使用linq to sql在Listview和DataGrid中添加映像
我有一个listview和一个DataGrid,其中包含带有图像的记录。我的问题是,当我尝试在listview或Datagrid中添加记录时,我只能添加字符串或数字,而不能添加图像。我试图通过OpenFile对话框找到它,并尝试分配给datagrid单元格,但如果搜索图像,我不知道在运行时分配什么。另一个问题是,我一直在绑定Datagrid的映像,但我尝试更新它并在运行时添加它 我的第二个问题是更新图像。如果有人知道添加和更新listview中的记录以及使用模板的Datagrid,那么请上帝保佑我,我尝试了一下,但无法修复。如果有谁是wpf的专家,请告诉我你的几分钟可以解决我的问题,请 我的.xmal文件是Wpf 如何在运行时使用linq to sql在Listview和DataGrid中添加映像,wpf,linq-to-sql,listview,Wpf,Linq To Sql,Listview,我有一个listview和一个DataGrid,其中包含带有图像的记录。我的问题是,当我尝试在listview或Datagrid中添加记录时,我只能添加字符串或数字,而不能添加图像。我试图通过OpenFile对话框找到它,并尝试分配给datagrid单元格,但如果搜索图像,我不知道在运行时分配什么。另一个问题是,我一直在绑定Datagrid的映像,但我尝试更新它并在运行时添加它 我的第二个问题是更新图像。如果有人知道添加和更新listview中的记录以及使用模板的Datagrid,那么请上帝保佑
<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();
}
}
}
添加
拯救
删除
眉头
我的.xaml.cs文件是
名称空间用户界面
{
///
///ViewClasses.xaml的交互逻辑
///
公共部分类ViewClasses:窗口
{
公共视图类()
{
初始化组件();
}私有DataClasses1DataContext db=新DataClasses1DataContext();
私有BindingListCollectionView自定义视图;
已加载私有无效窗口(对象发送器、路由目标)
{
DataClasses1DataContext db=新DataClasses1DataContext();
var custsInCA=来自c,单位为db.Students
选择c;
this.DataContext=custsInCA;
this.CustomerView=((BindingListCollectionView)(CollectionViewSource.GetDefaultView(this.DataContext));
}
私有void DataGrid_LoadingRow(对象发送方,Microsoft.Windows.Controls.DataGridRowEventArgs e)
{
}
已加载私有void数据网格(对象发送方,RoutedEventArgs e)
{
}
私有void btnSave_单击(对象发送方,路由目标)
{
尝试
{
this.db.SubmitChanges();
MessageBox.Show(“已保存”);
}
捕获(例外情况除外)
{
Show(例如ToString());
}
}
私有无效btnAdd_单击(对象发送者,路由目标e)
{
Student st=((Student)(this.CustomerView.AddNew());
st.LastName=“”;
this.CustomerView.CommitNew();
这个.DataGrid.ScrollIntoView(st);
}
私有无效BTN删除\单击(对象发送方,路由目标)
{
如果((this.CustomerView.CurrentPosition>-1))
{
this.CustomerView.RemoveAt(this.CustomerView.CurrentPosition);
}
}
私有无效浏览(对象发送者,路由目标)
{
Image image1=新图像();
Microsoft.Windows.Controls.DataGrid df=新的Microsoft.Windows.Controls.DataGrid();
Microsoft.Win32.OpenFileDialog文件选择器=新建Microsoft.Win32.OpenFileDialog();
fileChooser.Filter=“图像文件|*.jpg;*.gif;*.bmp;*.png;;*.jpeg”;
可为空的结果=fileChooser.ShowDialog();
如果(结果==真)
尝试
{
txtBrowseFile.Text=fileChooser.FileName;
}
catch{return;}
if(txtBrowseFile.Text.Trim().Length!=0)
{
BitmapImage src=新的BitmapImage();
src.BeginInit();
src.UriSource=新Uri(txtbrossefile.Text.Trim(),UriKind.Relative);
src.CacheOption=BitmapCacheOption.OnLoad;
src.EndInit();
图1.Source=src;
}
}
}
公共类图像转换器:IValueConverter
{
公共对象转换(对象值、类型targetType、对象参数、,
System.Globalization.culture(信息文化)
{
System.Data.Linq.binaryData=值为System.Data.Linq.binaryData;
//System.Data.Linq.Binary binaryData=value;//这里有第一个错误。如何将binaryData转换为对象??
如果(二进制数据==null)
{
返回null;
}
byte[]buffer=binaryData.ToArray();
如果(buffer.Length==0)
{
返回null;
}
BitmapImage res=新的BitmapImage();
res.BeginInit();
res.StreamSource=新系统IO.MemoryStream(缓冲区);
res.EndInit();
返回res;
}
公共对象转换回(对象值、类型targetType、对象参数、CultureInfo区域性)
{
抛出新的NotImplementedException();
}
}
}
我的问题是,我想在运行时添加记录,我可以添加其他记录,但不能添加图像,也不能在运行时在数据网格中更新图像。如果有
DataClasses1DataContext db = new DataClasses1DataContext();
var custsInCA = from c in db.Students
select c;
this.DataContext = custsInCA;
this.CustomerView = ((BindingListCollectionView)(CollectionViewSource.GetDefaultView(this.DataContext)));
public ObservableCollection<Student> Students = null;
public CollectionViewSource cvs = new CollectionViewSource;
public ViewClasses()
{
cvs.Source = Students;
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
//Open Context
DataClasses1DataContext db new DataClasses1DataContext();
//Populate ObservableCollection with objects
Students =
new ObservableCollection<Student>((from c in db.Students select c).ToList());
//Raise PropertyChangedNotification
OnPropertyChanged("Students");
}
<my:DataGrid ItemsSource="{Binding cvs}" Name="DataGrid">