在WPF中的DataGrid中显示图像路径

发布于 2024-12-01 20:28:12 字数 3376 浏览 1 评论 0原文

目前我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

星星的軌跡 2024-12-08 20:28:12

我看到你有这个模板。

<DataGridTemplateColumn Header="Image">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Image Source="{Binding Path=im_name, Mode=OneWay}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

您是否将绑定项“im_name”作为图像的实际 URL 作为字符串来源?
您可以上网吗?

如果这两个为真,您的图像就会显示。

把它带回到最基本的形式。在 Visual Studio 中创建一个新的 WPF 应用程序,制作一个测试应用程序,您将看到图像绑定并显示。

MainWindow.xaml

<Window x:Class="TestImage.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        >
    <StackPanel>
        <Image Name="MyImage" Source="{Binding ImagePath}" />
        <DataGrid Name="MyDataGrid" AutoGenerateColumns="False" EnableRowVirtualization="True"
                  ItemsSource="{Binding}" RowDetailsVisibilityMode="VisibleWhenSelected">
            <DataGrid.Columns>
                <DataGridTextColumn x:Name="IdColumn" Binding="{Binding Path=Id}" Header="Id" />
                <DataGridTextColumn x:Name="ImagePathColumn" Binding="{Binding Path=ImagePath}" 
                                    Header="Image Path" />
                <DataGridTemplateColumn x:Name="ImageColumn" Header="Image">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Image Source="{Binding Path=ImagePath}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </StackPanel>
</Window>

MainWindows.xaml.cs

using System;
using System.Windows;
using System.Windows.Media;
using System.Collections.ObjectModel;

namespace TestImage
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            MyData data1 = new MyData() { Id = 1, ImagePath = "http://www.rhyous.com/wp-content/themes/rhyous/swordtop.png" };
            MyImage.DataContext = data1;

            ObservableCollection<MyData> DataList = new ObservableCollection<MyData>();
            DataList.Add(data1);
            MyData data2 = data2 = new MyData() { Id = 2, ImagePath = "http://gigabit.com/images/whmcslogo.gif" };

            MyDataGrid.ItemsSource = DataList;
        }
    }

    public class MyData
    {
        public String ImagePath { get; set; }
        public int Id { get; set; }

    }
}

请参阅将 Source 绑定到字符串的 http 路径足以从给定的 Web 路径加载图像。

我希望这对你有帮助。

I see you have this template.

<DataGridTemplateColumn Header="Image">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Image Source="{Binding Path=im_name, Mode=OneWay}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

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

<Window x:Class="TestImage.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        >
    <StackPanel>
        <Image Name="MyImage" Source="{Binding ImagePath}" />
        <DataGrid Name="MyDataGrid" AutoGenerateColumns="False" EnableRowVirtualization="True"
                  ItemsSource="{Binding}" RowDetailsVisibilityMode="VisibleWhenSelected">
            <DataGrid.Columns>
                <DataGridTextColumn x:Name="IdColumn" Binding="{Binding Path=Id}" Header="Id" />
                <DataGridTextColumn x:Name="ImagePathColumn" Binding="{Binding Path=ImagePath}" 
                                    Header="Image Path" />
                <DataGridTemplateColumn x:Name="ImageColumn" Header="Image">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Image Source="{Binding Path=ImagePath}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </StackPanel>
</Window>

MainWindows.xaml.cs

using System;
using System.Windows;
using System.Windows.Media;
using System.Collections.ObjectModel;

namespace TestImage
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            MyData data1 = new MyData() { Id = 1, ImagePath = "http://www.rhyous.com/wp-content/themes/rhyous/swordtop.png" };
            MyImage.DataContext = data1;

            ObservableCollection<MyData> DataList = new ObservableCollection<MyData>();
            DataList.Add(data1);
            MyData data2 = data2 = new MyData() { Id = 2, ImagePath = "http://gigabit.com/images/whmcslogo.gif" };

            MyDataGrid.ItemsSource = DataList;
        }
    }

    public class MyData
    {
        public String ImagePath { get; set; }
        public int Id { get; set; }

    }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文