为了账号安全,请及时绑定邮箱和手机立即绑定

如何从文件名与 CollectionViewSource 中的项目匹配的本地文件夹插入图像

如何从文件名与 CollectionViewSource 中的项目匹配的本地文件夹插入图像

C#
慕姐4208626 2023-08-13 15:41:00
我正在 WPF 中构建我的第一个应用程序,并且尝试将本地文件夹中的图像添加到列表视图中。该文件夹包含文件名由 10 位数字组成的图像(例如 1234567890.jpg)。问题是图像 (1234567890.jpg) 应该与数据库中 EmployeeID 为 1234567890 的员工相匹配。所以我有 2 个不同的来源。我是编程新手,现在我已经在 Google 上花了 2 天时间,试图找到解决方案,但没有任何运气。我尝试合并到 CompositeCollection 中。我尝试过创建一个包含图像的列表。我想我已经尝试了一切。这是我到目前为止所拥有的:<Window.Resources>        <DataTemplate x:Key="EmployeeTemplate">            <Grid MaxWidth="500" Margin="3">                <Grid.ColumnDefinitions>                    <ColumnDefinition Width="Auto"/>                    <ColumnDefinition/>                </Grid.ColumnDefinitions>                <Image Name="employeeImage" Grid.Column="0" Width="50"                        Source=""/>                <StackPanel Grid.Column="1">                    <StackPanel Margin="3,0" Orientation="Horizontal">                        <TextBlock Text="{Binding LastName}"/>                        <TextBlock Text=", "/>                        <TextBlock Text="{Binding FirstName}"/>                    </StackPanel>                    <TextBlock Name="EmployeeIDTextBlock" Margin="3,0" Text="{Binding EmployeeID}"/>                    <TextBlock Margin="3,0" Text="{Binding Department.DepartmentName}"/>                </StackPanel>            </Grid>        </DataTemplate>        <CollectionViewSource x:Key="employeeViewSource" d:DesignSource="{d:DesignInstance {x:Type local:Employee}, CreateList=True}"/>    </Window.Resources><Grid>                    <Grid.ColumnDefinitions>                        <ColumnDefinition Width="Auto"/>                        <ColumnDefinition/>                    </Grid.ColumnDefinitions>                    <ListView x:Name="grid1" Grid.Column="0"                                 ItemTemplate="{StaticResource EmployeeTemplate}"                                ItemsSource="{Binding Source={StaticResource employeeViewSource}}"/>                </Grid>我想要的是将图片显示在列表中的每个员工面前。如果我问错了这个问题或遗漏了一些东西,我深表歉意,但这也是我在这里的第一个问题。我希望你能帮忙。
查看完整描述

1 回答

?
海绵宝宝撒

TA贡献1809条经验 获得超8个赞

如果我很好地理解您需要什么,您可以使用一个转换器,使用“EmployeeID”将为您返回一个 ImageSource:


class Imageconverter : IValueConverter

    {

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)

        {

            if (value == null)

                return null;

            string imgDir = @"\Imgs\";

            string fileName = System.IO.Path.Combine(imgDir,String.Format("{0}.jpg", value.ToString()));



            if (!System.IO.File.Exists(fileName))

                return null;



            BitmapImage src = new BitmapImage(new Uri(fileName, UriKind.Absolute));


            return src;

        }


        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)

        {

            throw new NotImplementedException();

        }

    }

为转换器创建资源:


<local:Imageconverter x:key="ImgConverter/>

并使用它:


<Image Name="employeeImage" Grid.Column="0" Width="50" 

                       Source="{Binding EmployeeID, Converter={StaticResource ImgConverter}}"/>



查看完整回答
反对 回复 2023-08-13
  • 1 回答
  • 0 关注
  • 63 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信