У меня есть просмотр списка, а также DataGrid с записями с изображениями.Моя проблема в том, что когда я пытаюсь добавить запись в просмотр списка или в таблицу данных, я могу добавить только строку или только цифры, но не могу добавить изображение.Я попытался найти его с помощью поля openfiledialog и попытаться назначить ячейку таблицы данных, но я не знаю, как назначить изображение во время выполнения, если я ищу изображение.Другая проблема заключается в том, что я связывал образ Datagrid, но я пытаюсь обновить его и добавить его во время выполнения.
И моя вторая проблема - обновление образа.Если кто-то знает о добавлении и обновлении записи в просмотре списка, а также в шаблоне с использованием Datagrid, пожалуйста, пожалуйста, ради Бога, помогите мне, но я не могу это исправить.Если кто-то, кто является экспертом по wpf, скажите, пожалуйста, что ваши несколько минут могут решить мою проблему, пожалуйста.
Мой файл .xmal
<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();
}
}
}
Моя проблема в том, что я хочу добавить записьво время выполнения я могу добавить другую запись, но не могу изображение, а также не могу обновить изображение во время выполнения в сетке данных.Если в wpf есть какой-либо эксперт, то, пожалуйста, переиграйте меня и, если возможно, воспроизведите меня полностью, чтобы я мог понять, что у меня проблема с затенением электрической нагрузки в моей стране, поэтому я не могу быстро ответить вам, поэтому, если возможно, ответьте мне дляМешок Божий Я просто желаю благородным людям тех, кто помогает мне.