C# 如何使用linq到sql在DataGrid中绑定映像和名字

C# 如何使用linq到sql在DataGrid中绑定映像和名字,c#,wpf,linq-to-sql,binding,C#,Wpf,Linq To Sql,Binding,我已经在Listview中绑定了图像和其他记录,但是我在使用linq到sql绑定DataGrid中的记录时遇到问题。请告诉我如何在DataGrid中绑定图像和名字。我尝试绑定它,但最终我的学生表中的结果为空意味着没有错误,但它没有显示任何记录我确信我有一些绑定问题如何解决请告诉我,谢谢阅读。这是我的wpf.xml文件,请查看并更正此填充中的错误 <Window x:Class="UI.ViewStudent" xmlns="http://schemas.microsoft.com/

我已经在Listview中绑定了图像和其他记录,但是我在使用linq到sql绑定DataGrid中的记录时遇到问题。请告诉我如何在DataGrid中绑定图像和名字。我尝试绑定它,但最终我的学生表中的结果为空意味着没有错误,但它没有显示任何记录我确信我有一些绑定问题如何解决请告诉我,谢谢阅读。这是我的wpf.xml文件,请查看并更正此填充中的错误

<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();
        }
    }
}
命名空间用户界面
{
/// 
///Pics.xaml的交互逻辑
/// 
公共部分类图片:窗口
{
公共图片()
{
初始化组件();
}
私有DataClasses1DataContext db=新DataClasses1DataContext();
私有BindingListCollectionView自定义视图;
已加载私有无效窗口(对象发送器、路由目标)
{
var custsInCA=来自c,单位为db.Students
其中c.StudentID==100
orderby c.LastName,c.FirstName
选择c;
this.DataContext=custsInCA;
this.CustomerView=((BindingListCollectionView)(CollectionViewSource.GetDefaultView(this.DataContext));
}
//公共字符串值{get;set;}
}
公共类ImageDataConverter: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();
}
}
}

我的问题是,我在DataGrid中以错误的方式绑定记录,因为我在编译后显示了网格,但没有记录没有图片请告诉我我的应用程序中出现了什么问题以及如何解决修复请在完整示例中告诉我如何使用linq到sql wpf绑定图像和名字检查后面是否有空格路径=

<Image Height="100" Source="{Binding Path= MyImage, 
     Converter={StaticResource ImageData1Converter}}" />


删除path=MyImageOh中的偶数空格后,什么也没有发生。。。转换器函数中的值类型是什么?你们能调试一下吗?我已经调试过了,但结果是我的学生对象中的null只意味着学生表中没有任何字段的读取。学生表是否有StudentId=100的图像。检查您的子句,其中c.StudentID==100。是的,它有StudentID==100,有图像,我在listview上尝试此查询,对于listview,结果是ok的,但是对于Datagride,它有绑定问题