首页 > 其他分享 >WPF Datagrid display via DataGridTemplateColumn

WPF Datagrid display via DataGridTemplateColumn

时间:2024-10-03 19:11:22浏览次数:8  
标签:via Windows imgsList System MainWindow Datagrid using WPF public

 <DataGridTemplateColumn Header="Image">
     <DataGridTemplateColumn.CellTemplate>
         <DataTemplate>
             <Image Source="{Binding DataContext.ImgUrl,
                 RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type DataGridRow}}}"
                 Width="200" Height="500"/>
         </DataTemplate>
     </DataGridTemplateColumn.CellTemplate>
 </DataGridTemplateColumn>

 

 

 

 

 

 

 

//xaml
<Window x:Class="WpfApp4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp4"
        mc:Ignorable="d"
        WindowState="Maximized"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <local:BooksData x:Key="booksData"/>
    </Window.Resources>
    <Grid>
        <DataGrid AutoGenerateColumns="False"
                  CanUserAddRows="False"
                  VirtualizingPanel.IsContainerVirtualizable="True"
                  VirtualizingPanel.IsVirtualizing="True"
                  VirtualizingPanel.VirtualizationMode="Recycling"
                  ItemsSource="{StaticResource booksData}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Id" Binding="{Binding Id}"/>
                <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
                <DataGridTemplateColumn Header="Image">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Image Source="{Binding DataContext.ImgUrl,
                                RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type DataGridRow}}}"
                                Width="200" Height="500"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>





//xaml.cs
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;

namespace WpfApp4
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }

    public class BooksData:ObservableCollection<Book>
    {
        public BooksData()
        {
            var imgsList = Directory.GetFiles(@"../../Images");
            if(imgsList!=null && imgsList.Any())
            {
                int imgsCnt = imgsList.Count();
                for(int i=0;i<100000;i++)
                {
                    this.Add(new Book()
                    {
                        Id = i + 1,
                        Name = $"Name_{i + 1}",
                        ImgUrl = imgsList[i % imgsCnt]
                    });
                }
            }
        }
    }

    public class Book
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public string ImgUrl { get; set; }
    }
}

 

标签:via,Windows,imgsList,System,MainWindow,Datagrid,using,WPF,public
From: https://www.cnblogs.com/Fred1987/p/18445907

相关文章

  • WPF FindResource,Resource[key] SystemColors TryFindResource
    //xaml<Windowx:Class="WpfApp3.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.micr......
  • WPF image via web url or uri
    Thebasicroadmapistodownloadwebimageatfirst,second convertitintomemeorystream,thirdassignthememorystreamtobitmapimageasStreamSource. //xaml<Windowx:Class="WpfApp2.MainWindow"xmlns="http://schemas.micro......
  • WPF Click Window show XY coordinates in MVVM via InvokeCommandAction of behavior
    1.Install Microsoft.Xaml.Behaviors.Wpf  2.<behavior:Interaction.Triggers><behavior:EventTriggerEventName="MouseDown"><behavior:InvokeCommandActionCommand="{BindingClickCommand}"......
  • WPF MVVM behavior CallMethodAction InvokeCommandAction
    1.Install  Microsoft.Xaml.Behaviors.Wpf 2.xmlns:behavior="http://schemas.microsoft.com/xaml/behaviors"<behavior:Interaction.Triggers><behavior:EventTriggerEventName="KeyDown"><behavior:CallMet......
  • 欢乐国庆:SciChart 8.5 for WPF Crack
    WPFChartPerformanceSciChart’slegendaryWPFChartperformanceisenabledbyourproprietary,in-house,C++crossplatform3Dgraphicsengine,VisualXccelerator.TheengineisentirelyhardwareacceleratedtargettingDirectXonWindowsplatform.Youw......
  • (六)WPF数据驱动模式
     WPF开发方式; MVVM(ModelViewViewModel)1.绑定XAML数据方式  在 XAML中添加绑定数据和绑定的操作属性        Content="{BindingMyVar}" 在XAML对应了的窗体类的构造函数添加数据绑定        this.DataContext=mainViewModel;//让此页面的数据取......
  • wpf ObservableCollection筛选功能
    viewmodel中定义原始数据及筛选后的数据,筛选后的数据类型为ICollectionView//原始数据列表publicObservableCollection<SchoolOutDto>SchoolList{get;set;}///<summary>///筛选数据后的列表///</summary>publicICollectionViewFilterSchoolList{get;set;}......
  • WPF publish release application
    Rightclickproject/Properties/Publish   ThenclickPublishWizardstepbystep,thenwillcreateasetup.exefileinpublishfolder Clickthesetup.exefileandtheninstalltheapplicationstepbystep,finallyitshowin ControlPanel\AllContro......
  • WPF relative uri
    <ImageGrid.Column="0"Source="pack://application:,,,/WpfApp431;component/Images/1.jpg"RenderOptions.BitmapScalingMode="Fant"/><ImageGrid.Column="1"Source="pack://applic......
  • WPF mutex single instance
    privatestaticMutexmtx=null;publicMainWindow(){boolisCreateNew;stringappName=System.IO.Path.GetFileName(System.Reflection.Assembly.GetExecutingAssembly().Location);mtx=newMutex(true,appName,outisCreateNew);if(!isCreate......