首页 > 其他分享 >WPF ComboBox DataTemplate Image

WPF ComboBox DataTemplate Image

时间:2024-09-21 20:50:21浏览次数:7  
标签:Windows ComboBox imgsList System public using WPF MainWindow DataTemplate

<ComboBox 
          ItemsSource="{StaticResource dataList}"
          SelectedIndex="0"
          VirtualizingPanel.IsContainerVirtualizable="True"
          VirtualizingPanel.IsVirtualizing="True"
          VirtualizingPanel.VirtualizationMode="Recycling">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <Image Source="{Binding SelectedItem,RelativeSource=
                {RelativeSource Mode=FindAncestor,AncestorType={x:Type ComboBox}}}"
                Stretch="Uniform"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

//xaml
<Window x:Class="WpfApp394.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:WpfApp394"
        mc:Ignorable="d" 
        WindowState="Maximized"
        Title="MainWindow">
    <Window.Resources>
        <local:DataList x:Key="dataList"/>
    </Window.Resources>
    <Grid>
        <ComboBox 
                  ItemsSource="{StaticResource dataList}"
                  SelectedIndex="0"
                  VirtualizingPanel.IsContainerVirtualizable="True"
                  VirtualizingPanel.IsVirtualizing="True"
                  VirtualizingPanel.VirtualizationMode="Recycling">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <Image Source="{Binding SelectedItem,RelativeSource=
                        {RelativeSource Mode=FindAncestor,AncestorType={x:Type ComboBox}}}"
                        Stretch="Uniform"/>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
    </Grid>
</Window>



//cs
using System;
using System.Collections.Generic;
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 WpfApp394
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }

    public class DataList:List<string>
    {
        public DataList()
        {
            var imgsList = Directory.GetFiles(@"../../Images");
            if(imgsList!=null && imgsList.Any())
            {
                this.AddRange(imgsList);
            }
        }
    }
}

 

标签:Windows,ComboBox,imgsList,System,public,using,WPF,MainWindow,DataTemplate
From: https://www.cnblogs.com/Fred1987/p/18424497

相关文章

  • WPF render image, scaletransfrom and translatertransform
    <Windowx:Class="WpfApp392.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft......
  • WPF Combobox switch up and down then show the big picture in the right part
    <ComboBoxx:Name="cbx"Grid.Row="0"Grid.Column="0"SelectedIndex="0"ItemsSource="{StaticResourcebooksData}"FontSize="20"......
  • WPF Combobox show Image and textblock via DataTemplate
    <ComboBoxGrid.Row="0"Grid.Column="0"SelectedIndex="0"ItemsSource="{StaticResourcebooksData}"FontSize="20"Foreground=&quo......
  • 不可不知的WPF动画(Animation)
    在WPF开发应用中,动画不仅可以引起用户的注意与兴趣,而且还使软件更加便于使用。前面几篇文章讲解了画笔(Brush),形状(Shape),几何图形(Geometry),变换(Transform)等相关内容,今天继续讲解动画相关内容和知识点,仅供学习分享使用,如有不足之处,还请指正。 什么是动画? 动画是快速循环播放一系......
  • handycontrol的CheckComboBox的SelectedItems顺序
    【实现效果】【问题】handycontrol的CheckComboBox没有SelectedItems这一项:当保存下来的选中项,需要在下次打开的时候加载,而handycontrol的CheckComboBox没有SelectedItems,于是就先解决如何拿到绑定SelectedItems,通过附加属性的方式:WPF使用附加属性来绑定ListBox的SelectedIt......
  • WPF dynamically generate grid with calculated rows and columns, put the custom c
    privatevoidFillGridRandomly(){rowsColsSet=newHashSet<XY>();if(gd!=null){gd.Children.Clear();}for(inti=0;i<50;i++){while(true){XYxy=newXY();......
  • Wpf使用NLog将日志输出到LogViewer
    1LogViewerLogViewer是通过UDP传输的高性能实时log查看器。具有一下特性:通过UDP读取日志通过文件导入日志导出日志到一个文件中排序、过滤(日志树,日志等级)和查找突出显示搜索文本从UPD接收日志时忽略IP地址列表多接收器支持多种颜色主题项目地址:https://github.com/......
  • 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 ListBox ListBox.ItemTemplate DataTemplate Custom UserControl
    <ListBox.ItemTemplate><DataTemplate><local:ImageTbkUCImgUrl="{BindingDataContext.ImgUrl,RelativeSource={RelativeSourceMode=FindAncestor,AncestorType=ListBoxItem}}"U......
  • 将 WPF 窗口嵌入到 MFC 窗口中
    背景有一个现存的MFC项目,需要在里面添加新的UI界面,使用MFC开发太费劲,完全使用WPF再重写一遍,时间上不允许。可以考虑直接将WPF窗口嵌入到MFC窗口中,以下是探索过程中的一些记录。......