首页 > 其他分享 >WPF Combobox show Image and textblock via DataTemplate

WPF Combobox show Image and textblock via DataTemplate

时间:2024-09-21 16:50:51浏览次数:7  
标签:via show Windows Combobox imgsList System MainWindow using public

 <ComboBox 
           Grid.Row="0"
           Grid.Column="0"
           SelectedIndex="0"
           ItemsSource="{StaticResource booksData}"                  
           FontSize="20" Foreground="Red" 
           VerticalAlignment="Stretch" 
           HorizontalAlignment="Stretch" 
           BorderBrush="Blue" 
           BorderThickness="3"
           VirtualizingPanel.IsContainerVirtualizable="True"
           VirtualizingPanel.IsVirtualizing="True"
           VirtualizingPanel.CacheLength="10"
           VirtualizingPanel.CacheLengthUnit="Item"
           VirtualizingPanel.VirtualizationMode="Recycling">
     <ComboBox.ItemTemplate>
         <DataTemplate>
             <Grid>
                 <Grid.RowDefinitions>
                     <RowDefinition/>
                     <RowDefinition Height="10*"/>
                     <RowDefinition/>
                 </Grid.RowDefinitions> 
                 <TextBlock Text="{Binding Name}" Grid.Row="0"/>
                 <TextBlock Text="{Binding ImgUrl}" Grid.Row="2"/>
                 <Image Source="{Binding ImgUrl}" Width="200" Height="500" 
                        Grid.Row="1"/>                        
             </Grid>
         </DataTemplate>
     </ComboBox.ItemTemplate>
 </ComboBox> 

 

 

 

 

 

 

 

 

//xaml
<Window x:Class="WpfApp391.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:WpfApp391"
        mc:Ignorable="d"
        WindowState="Maximized"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <local:BooksData x:Key="booksData"/>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="2*"/>
        </Grid.ColumnDefinitions>
        <ComboBox 
                  Grid.Row="0"
                  Grid.Column="0"
                  SelectedIndex="0"
                  ItemsSource="{StaticResource booksData}"                  
                  FontSize="20" Foreground="Red" 
                  VerticalAlignment="Stretch" 
                  HorizontalAlignment="Stretch" 
                  BorderBrush="Blue" 
                  BorderThickness="3"
                  VirtualizingPanel.IsContainerVirtualizable="True"
                  VirtualizingPanel.IsVirtualizing="True"
                  VirtualizingPanel.CacheLength="10"
                  VirtualizingPanel.CacheLengthUnit="Item"
                  VirtualizingPanel.VirtualizationMode="Recycling">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition/>
                            <RowDefinition Height="10*"/>
                            <RowDefinition/>
                        </Grid.RowDefinitions> 
                        <TextBlock Text="{Binding Name}" Grid.Row="0"/>
                        <TextBlock Text="{Binding ImgUrl}" Grid.Row="2"/>
                        <Image Source="{Binding ImgUrl}" Width="200" Height="500" 
                               Grid.Row="1"/>
                    </Grid>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox> 
    </Grid>
</Window>


//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 WpfApp391
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            RenderOptions.ProcessRenderMode = System.Windows.Interop.RenderMode.SoftwareOnly;
        }
    }

    public class BooksData:ObservableCollection<Book>
    {
        public BooksData()
        {
            var imgsList = Directory.GetFiles(@"../../Images");
            if(imgsList==null || !imgsList.Any())
            {
                return;
            }

            int imgsCount = imgsList.Count();
            for(int i=0;i< imgsCount; i++)
            {
                this.Add(new Book()
                { 
                    Id=i+1,
                    Name=$"Name_{i+1}",
                    ImgUrl = $"{imgsList[i]}"
                });
            }
        }
    }

    public class Book
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string ImgUrl { get; set; }
    }
}

 

标签:via,show,Windows,Combobox,imgsList,System,MainWindow,using,public
From: https://www.cnblogs.com/Fred1987/p/18424207

相关文章

  • handycontrol的CheckComboBox的SelectedItems顺序
    【实现效果】【问题】handycontrol的CheckComboBox没有SelectedItems这一项:当保存下来的选中项,需要在下次打开的时候加载,而handycontrol的CheckComboBox没有SelectedItems,于是就先解决如何拿到绑定SelectedItems,通过附加属性的方式:WPF使用附加属性来绑定ListBox的SelectedIt......
  • WPF Canvas show custom control with ellipse filled with image and text,peridoica
    //customcontrol<UserControlx:Class="WpfApp389.ElpImageTbk"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"......
  • WPF Element Width Height is percent of Parent element via converter ,converterpa
    //converterusingSystem;usingSystem.Collections.Generic;usingSystem.Globalization;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Data;namespaceWpfApp380{publicclassSizeConverter:IValueConverter......
  • Imitating Language via Scalable Inverse Reinforcement Learning
    本文是LLM系列文章,针对《ImitatingLanguageviaScalableInverseReinforcementLearning》的翻译。通过可扩展的逆向强化学习模仿语言摘要1引言2方法3实验4相关工作5讨论6结论摘要大多数语言模型训练都建立在模仿学习的基础上。它涵盖了预训练、监......
  • General OCR Theory: Towards OCR-2.0 via a Unified End-to-end Model
    摘要传统的OCR系统(OCR-1.0)越来越无法满足人们对智能处理人造光学字符的需求。在本文中,我们将所有人造光学信号(例如,普通文本、数学/分子公式、表格、图表、乐谱,甚至是几何形状)统称为“字符”,并提出了通用OCR理论以及一个优秀的模型,即GOT,以促进OCR-2.0的到来。GOT拥有5.8亿参......
  • Trivial, standard-layout, POD, and literal types
    转自:Trivial,standard-layout,POD,andliteraltypeshttps://learn.microsoft.com/en-us/cpp/cpp/trivial-standard-layout-and-pod-types?view=msvc-170微软这篇文章写的很详尽,也配有代码实例Trivial,standard-layout,POD,andliteraltypes    Thetermlayoutre......
  • WPF overlay on the icon in the taskbar via TaskbarItemInfo
    <Windowx:Class="WpfApp372.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft......
  • [Vue] v-html、v-show、v-if 的区别
    v-if作用:根据条件动态创建或销毁DOM元素。原理:v-if实际上是按需渲染的,它会根据表达式的真假值来决定是否在DOM树中插入或移除元素。如果条件为false,该元素不会存在于DOM中。性能:因为v-if会真正地添加或移除元素,因此它在初次渲染时的开销较大,特别是当需要频繁切换时会......
  • [CTFshow] 文件包含 78~88,116~117
    web78if(isset($_GET['file'])){$file=$_GET['file'];include($file);}else{highlight_file(__FILE__);}php伪协议,data://数据流封装器,以传递相应格式的数据。通常可以用来执行PHP代码?file=data://text/plain,<?=system('ls')?>?file=dat......
  • 一款类excel可进行显示、在线编辑的纯js表格TableShow控件
        在进行前端显示设计时,传统的方法是以分页显示,逐条提取后修改及保存,非常不利于用户连贯阅读及在线修改。因此,本人将类似excel的一些table表格在线卷动显示、修改及集中保存功能进行了尝试,封装成了一个纯js控件,只通过一句代码进行调用,将数据库查询结果集进行显示和添......