首页 > 其他分享 >WPF Combobox ObjectDataProvider MethodName ObjectType ObjectDataProvider.MethodParameters

WPF Combobox ObjectDataProvider MethodName ObjectType ObjectDataProvider.MethodParameters

时间:2024-09-22 14:25:05浏览次数:7  
标签:MethodName Windows Combobox imgsList System ObjectDataProvider using public

<Window.Resources>
    <ObjectDataProvider x:Key="kindEnum" MethodName="GetValues"
                        ObjectType="{x:Type sys:Enum}">
        <ObjectDataProvider.MethodParameters>
            <x:Type Type="local:BookKindEnum"/>
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
    <local:BooksData x:Key="booksData"/>
</Window.Resources>


<DataGridComboBoxColumn Header="Kind" 
                        SelectedItemBinding="{Binding BookKind}"
                        ItemsSource="{Binding Source={StaticResource kindEnum}}"/>

 

 

 

 

 

 

 

 

 

 

 

//Full code
<Window x:Class="WpfApp397.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:WpfApp397"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d" WindowState="Maximized"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <ObjectDataProvider x:Key="kindEnum" MethodName="GetValues"
                            ObjectType="{x:Type sys:Enum}">
            <ObjectDataProvider.MethodParameters>
                <x:Type Type="local:BookKindEnum"/>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
        <local:BooksData x:Key="booksData"/>
    </Window.Resources>
    <Grid>
        <DataGrid ItemsSource="{StaticResource booksData}"
                  BorderBrush="Blue" BorderThickness="3"
                  VirtualizingPanel.IsContainerVirtualizable="True"
                  VirtualizingPanel.IsVirtualizing="True"
                  VirtualizingPanel.VirtualizationMode="Recycling"
                  AutoGenerateColumns="False"
                  CanUserAddRows="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Id" Binding="{Binding Id}" SortDirection="Ascending" />
                <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
                <DataGridComboBoxColumn Header="Kind" 
                                        SelectedItemBinding="{Binding BookKind}"
                                        ItemsSource="{Binding Source={StaticResource kindEnum}}"/>
                <DataGridTemplateColumn Header="Image">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Viewbox>
                                <Image Source="{Binding ImgUrl}" Width="200" Height="500"/>
                            </Viewbox>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </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 WpfApp397
{
    /// <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 imgsCount = imgsList.Count();
                for(int i=0;i<1000000;i++)
                {
                    var bk = new Book()
                    {
                        Id = i + 1,
                        Name = $"Name_{i + 1}",
                        ImgUrl = imgsList[i % imgsCount]                        
                    };
                    bk.BookKind = bk.GetBookKindEnum(i);
                    this.Add(bk);
                }
            }
        }
    }

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

        public string Name { get; set; }

        public string ImgUrl { get; set; }

        public BookKindEnum BookKind { get; set; }

        public BookKindEnum GetBookKindEnum(int idx)
        {
            switch(idx)
            {
                case 0:
                    return BookKindEnum.Science;
                case 1:
                    return BookKindEnum.Technology;
                case 2:
                    return BookKindEnum.Engineering;
                default:
                    return BookKindEnum.Mathematics;
            }
        }
    }

    public enum BookKindEnum
    {
        Science,
        Technology,
        Engineering,
        Mathematics
    }
}

 

标签:MethodName,Windows,Combobox,imgsList,System,ObjectDataProvider,using,public
From: https://www.cnblogs.com/Fred1987/p/18425246

相关文章

  • WPF ComboBox DataTemplate Image
    <ComboBoxItemsSource="{StaticResourcedataList}"SelectedIndex="0"VirtualizingPanel.IsContainerVirtualizable="True"VirtualizingPanel.IsVirtualizing="True"Vir......
  • 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......
  • handycontrol的CheckComboBox的SelectedItems顺序
    【实现效果】【问题】handycontrol的CheckComboBox没有SelectedItems这一项:当保存下来的选中项,需要在下次打开的时候加载,而handycontrol的CheckComboBox没有SelectedItems,于是就先解决如何拿到绑定SelectedItems,通过附加属性的方式:WPF使用附加属性来绑定ListBox的SelectedIt......
  • <Rust>egui学习之小部件(九):如何在窗口中添加下拉列表combobox部件?
    前言本专栏是关于Rust的GUI库egui的部件讲解及应用实例分析,主要讲解egui的源代码、部件属性、如何应用。环境配置系统:windows平台:visualstudiocode语言:rust库:egui、eframe概述本文是本专栏的第九篇博文,主要讲述下拉列表部件combobox的使用。事实上,类似于iced,egui......
  • shadcn/ui Combobox command 组合筛选下拉
    <Popoveropen={open}onOpenChange={setOpen}><PopoverTriggerasChild><Buttonrole="combobox"aria-expanded={open}className="w-fulljustify-betweenh-11bg-back......
  • 27 Combobox组件
    Tkinterttk.Combobox组件使用指南ttk.Combobox是Tkinter的一个高级控件,它结合了文本框和下拉列表的功能,允许用户从预定义的选项列表中选择一个值。ttk模块是Tkinter的一个扩展,提供了更现代的控件外观和行为。以下是对ttk.Combobox组件的详细说明和一个使用案例。......
  • StringGrid单元格绑定ComboBox、DateTimePicker或窗口传值
    一、初始化控件状态procedureTForm7.FormCreate(Sender:TObject);beginwithStringGrid1dobeginColWidths[0]:=15;Cells[1,0]:='Combobox';ColWidths[1]:=100;Cells[2,0]:='DateTimePicker';ColWidths[2]:=100;......
  • GridViewComboBoxColumn设置DataTypeConverter
    GridView中的GridViewComboBoxColumn列,如果需要使用TypeConverter将非字符串类型的数据源转换为字符串进行展示,可按如下几步进行:例如,数据源为如下枚举类型:publicenumMyColor{Red,Yellow,Green}展示的时候,需要转换为汉字,先定义如下类型,作为GridViewComboBo......
  • QComboBox列表设计
    我想自定义QComboBox列表背景。当我悬停时,我需要一个圆角框。我已附上当前图片和我需要的内容。这是我的样式表代码QComboBox{border:1pxsolidblack;background-color:white;font-size:15pt;padding:1px18px1px3px;}QComboBox:......