在WPF中的DataGrid中显示图像路径
目前我有一个 SQL Server 数据库,我希望在 WPF 的 DataGrid 中显示文章及其各自的图像。获取文章没有问题,但是我在获取和显示图像时遇到问题。由于这是一个相当古老的项目,图像只是文件名而不是 blob,因此我还需要显示网站的路径。例如 www.mysite.com/images/imagename。
我正在使用 EF,在模型中,我有一种方法来检索文章 (GetAllArticles),然后另外两种方法,一种用于按页检索图像,另一种用于检索图像。我可以使用这两种方法的视图,但我使用的是 LINQ,并且不太熟悉如何将这两种方法组合在一起。
所以模型是这样的:-
public List<HS_Articles>GetAllArticles()
{
var res = from art in HSEntities.HS_Articles select art;
return res.ToList();
}
public List<HS_Images_Pages> GetImagesByPage(int pageId, int subPageId)
{
var res = HSEntities.HS_Images_Pages.Where(img => img.im_page_id == pageId && img.sub_page_id == subPageId);
return res.ToList();
}
public HS_Images GetImage(int imgId)
{
var res = HSEntities.HS_Images.Where(img => img.im_id == imgId);
return res as HS_Images;
}
在实际的 WPF 中,我按如下方式绑定数据网格:-
private void LoadArticles()
{
var articlesDal = new ArticlesDAL();
var items = new List<HS_Articles>();
items = articlesDal.GetAllArticles();
dgArticles.ItemsSource = items;
dgArticles.Items.Refresh();
}
数据网格看起来像这样:-
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=ArticleID}" Header="ID" SortMemberPath="ArticleID" Width="30" />
<DataGridTextColumn Binding="{Binding Path=Title}" Header="Title" SortMemberPath="Abstract" Width="250">
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="TextWrapping" Value="NoWrap" />
<Setter Property="TextTrimming" Value="CharacterEllipsis" />
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTextColumn Header="Date" SortMemberPath="AddedDate" Binding="{Binding AddedDate}" Width="150" />
<DataGridTextColumn Binding="{Binding Path=Abstract}" Header="Abstract" SortMemberPath="Abstract" Width="450">
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="TextWrapping" Value="NoWrap" />
<Setter Property="TextTrimming" Value="CharacterEllipsis" />
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Path=AddedBy}" Header="Added By" SortMemberPath="AddedBy" Width="150" />
<DataGridTemplateColumn Header="Image">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding Path=im_name, Mode=OneWay}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
你能告诉我你会如何做到这一点吗?
感谢您的帮助和时间
At the moment I have an SQL Server DB, and I wish to display the Articles and their respective image in a DataGrid in WPF. To get the Articles is no problem, however I have a problem to get and display the image. As this is quite an old project, the images are just filenames and not blobs, so I need to display also the path to the website. For example www.mysite.com/images/imagename.
I am using EF, and in the model, I have a method to retrieve the Articles (GetAllArticles) and then another 2 methods, one to retrieve imagesbypage, and another to retrieve the image. I could use a view for the 2 methods but I am using LINQ and am not very familiar on how to put these 2 methods together.
So the model is like this :-
public List<HS_Articles>GetAllArticles()
{
var res = from art in HSEntities.HS_Articles select art;
return res.ToList();
}
public List<HS_Images_Pages> GetImagesByPage(int pageId, int subPageId)
{
var res = HSEntities.HS_Images_Pages.Where(img => img.im_page_id == pageId && img.sub_page_id == subPageId);
return res.ToList();
}
public HS_Images GetImage(int imgId)
{
var res = HSEntities.HS_Images.Where(img => img.im_id == imgId);
return res as HS_Images;
}
In the actual WPF, I am binding the Datagrid as follows :-
private void LoadArticles()
{
var articlesDal = new ArticlesDAL();
var items = new List<HS_Articles>();
items = articlesDal.GetAllArticles();
dgArticles.ItemsSource = items;
dgArticles.Items.Refresh();
}
And the Datagrid looks like this :-
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=ArticleID}" Header="ID" SortMemberPath="ArticleID" Width="30" />
<DataGridTextColumn Binding="{Binding Path=Title}" Header="Title" SortMemberPath="Abstract" Width="250">
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="TextWrapping" Value="NoWrap" />
<Setter Property="TextTrimming" Value="CharacterEllipsis" />
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTextColumn Header="Date" SortMemberPath="AddedDate" Binding="{Binding AddedDate}" Width="150" />
<DataGridTextColumn Binding="{Binding Path=Abstract}" Header="Abstract" SortMemberPath="Abstract" Width="450">
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="TextWrapping" Value="NoWrap" />
<Setter Property="TextTrimming" Value="CharacterEllipsis" />
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Path=AddedBy}" Header="Added By" SortMemberPath="AddedBy" Width="150" />
<DataGridTemplateColumn Header="Image">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding Path=im_name, Mode=OneWay}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
Can you please tell me how you would do this?
Thanks for your help and time
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我看到你有这个模板。
您是否将绑定项“im_name”作为图像的实际 URL 作为字符串来源?
您可以上网吗?
如果这两个为真,您的图像就会显示。
把它带回到最基本的形式。在 Visual Studio 中创建一个新的 WPF 应用程序,制作一个测试应用程序,您将看到图像绑定并显示。
MainWindow.xaml
MainWindows.xaml.cs
请参阅将 Source 绑定到字符串的 http 路径足以从给定的 Web 路径加载图像。
我希望这对你有帮助。
I see you have this template.
Is you source binding item "im_name" the actual URL to your image as a string?
And do you have internet access?
If those two are true your image will display.
Take it back to the most basic form. Create a new WPF Application in Visual Studio make a test app and you will see the image binds and displays.
MainWindow.xaml
MainWindows.xaml.cs
See the Source bind to the http path of the string is enough to load the image from the web path given.
I hope this helps you.