Как привязать изображение и имя в DataGrid с помощью linq to sql - PullRequest
1 голос
/ 10 января 2012

Я был Привязать изображение и другую запись в ListView, но у меня есть проблема с привязкой записи в DataGrid с использованием linq для sql. Пожалуйста, скажите мне, как связать изображение и имя в DataGrid. Я пытаюсь связать его, но, наконец, результат в моей ученической таблице равен нулю, означает отсутствие ошибки, но он не показывает записи. Я уверен, что у меня есть какая-то проблема с привязкой. Как я могу ее решить, пожалуйста, сообщите мне и спасибо за чтение. Вот мой .xml файл wpf, пожалуйста, посмотрите и исправьте ошибки на этом заполнении

<Window x:Class="UI.ViewStudent"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:UI"
    Title="ViewStudent" Height="577" Width="415" xmlns:my="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit">
    <Window.Resources >
        <local:ImageDataConverter x:Key="ImageData1Converter"/>
    </Window.Resources>

    <Grid Height="540">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="9*" />
            <ColumnDefinition Width="384*" />
        </Grid.ColumnDefinitions>
        <my:DataGrid ItemsSource="{Binding }" AutoGenerateColumns="False" Grid.Column="1" Margin="32,0,53,0" Name="dataGrid1" Height="200" VerticalAlignment="Top">
            <my:DataGrid.Columns>
                <my:DataGridTemplateColumn Header="FirstName">
                    <my:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                         <TextBox Text="{Binding Path=FirstName}" 
                                         Margin="-6,0,-6,0"/>
                        </DataTemplate>
                    </my:DataGridTemplateColumn.CellTemplate>
                </my:DataGridTemplateColumn>

                <my:DataGridTemplateColumn Header="Imagess">
                    <my:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Image Height="100" Source="{Binding Path= MyImage, Converter={StaticResource ImageData1Converter}}" />
                        </DataTemplate>
                    </my:DataGridTemplateColumn.CellTemplate>
                </my:DataGridTemplateColumn>
            </my:DataGrid.Columns>
        </my:DataGrid>
    </Grid >
</Window>

А для моего .xml.cs файла это

namespace UI
{
    /// <summary>
    /// Interaction logic for Pics.xaml
    /// </summary>
    public partial class Pics : Window
    {
        public Pics()
        {
            InitializeComponent();
        }
        private DataClasses1DataContext db = new DataClasses1DataContext();

        private BindingListCollectionView CustomerView;

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var custsInCA = from c in db.Students
                            where c.StudentID  == 100
                            orderby c.LastName, c.FirstName
                            select c;

            this.DataContext = custsInCA;
            this.CustomerView = ((BindingListCollectionView)(CollectionViewSource.GetDefaultView(this.DataContext)));
        }

        //public string value { get; set; }
    }

    public class ImageDataConverter : 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();
        }
    }
}

Моя проблема в том, что я неправильно связываю запись в DataGrid из-за того, что после компиляции я показываю Grid, но без записи нет изображения, скажите, пожалуйста, что не так в моем приложении, и как решить, как это исправить, скажите мне в более полный пример, как связать изображение и имя, используя linq для sql wpf

1 Ответ

0 голосов
/ 10 января 2012

Проверьте, есть ли место после Путь =

<Image Height="100" Source="{Binding Path= MyImage, 
     Converter={StaticResource ImageData1Converter}}" />
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...