首页 > 其他分享 >WPF ComboBox multiselect via ControlTemplate of ComboxItem

WPF ComboBox multiselect via ControlTemplate of ComboxItem

时间:2024-12-27 23:19:54浏览次数:12  
标签:via Windows ComboBox bmi System ComboxItem new using public

<Window x:Class="WpfApp99.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:WpfApp99"
        WindowState="Maximized"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <Style TargetType="{x:Type TextBlock}" x:Key="tbkStyle">
            <Setter Property="TextWrapping" Value="Wrap"/>            
        </Style>
        <Style TargetType="ComboBoxItem" x:Key="cbxItemStyle">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ComboBoxItem}">
                        <Border Height="100" Width="1300">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="100"/>
                                    <ColumnDefinition Width="100"/>
                                    <ColumnDefinition/>
                                </Grid.ColumnDefinitions>
                                <CheckBox Grid.Column="0" IsChecked="{Binding IsSTEM}" 
                                          IsThreeState="False"/>
                                <TextBlock Grid.Column="1" Text="{Binding Id}" />
                                <TextBlock Grid.Column="2" Text="{Binding ISBN}" 
                                           Style="{StaticResource tbkStyle}"/>
                            </Grid>
                            <Border.Background>
                                <ImageBrush ImageSource="{Binding ImgSource}"
                                            RenderOptions.BitmapScalingMode="Fant"
                                            Stretch="Uniform"/>
                            </Border.Background>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Foreground" Value="Red"/>
                                <Setter Property="FontSize" Value="30"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>                    
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <ComboBox Width="1300" 
                  Height="100"
                  MaxDropDownHeight="700"
                  HorizontalAlignment="Left"
                  VerticalAlignment="Top"
                  ItemsSource="{Binding BooksCollection,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                  ItemContainerStyle="{StaticResource cbxItemStyle}"/>
    </Grid>
</Window>



//cs
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
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;

namespace WpfApp99
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            var vm = new BookVM();
            this.DataContext = vm;
        }
    }

    public class BookVM : INotifyPropertyChanged
    {
        public BookVM()
        {
            InitData();
        }

        private void InitData()
        {
            var imgs = Directory.GetFiles(@"../../../Images");
            if (imgs != null && imgs.Any())
            {
                int imgsCount = imgs.Count();
                BooksCollection = new ObservableCollection<Book>();
                for (int i = 0; i < imgsCount; i++)
                {
                    BooksCollection.Add(new Book()
                    {
                        Id = i + 1,
                        IsSelected=false,
                        ISBN = $"ISBN_{Guid.NewGuid().ToString("N")}",
                        ImgSource = GetImageSourceViaUrl(imgs[i%imgsCount])
                    });
                }
            }
        }

        private ImageSource GetImageSourceViaUrl(string imgUrl)
        {
            BitmapImage bmi = new BitmapImage();            
            bmi.BeginInit();
            bmi.UriSource = new Uri(imgUrl, UriKind.RelativeOrAbsolute);
            bmi.EndInit();
            if(bmi.CanFreeze)
            {
                bmi.Freeze();
            }
            return bmi;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private ObservableCollection<Book> booksCollection;
        public ObservableCollection<Book> BooksCollection
        {
            get
            {
                return booksCollection ?? (new ObservableCollection<Book>());
            }
            set
            {
                if (value != booksCollection)
                {
                    booksCollection = value;
                    OnPropertyChanged();
                }
            }
        }
    }

    public class Book
    {
        public int Id { get; set; }
        public bool IsSelected { get; set; }
        public string ISBN { get; set; }
        public ImageSource ImgSource { get; set; }

    }
}

 

 

 

 

 

 

 

 

 

 

标签:via,Windows,ComboBox,bmi,System,ComboxItem,new,using,public
From: https://www.cnblogs.com/Fred1987/p/18636889

相关文章

  • WPF Combobox屏蔽按上下键切换选项
    最近在项目中有一个需求是Combobox可以进行编辑,类似于多行文本框一样进行编辑,在编辑的过程中可能需要上下键切换当前的行,但是combobox的上下键对应了切换选项,所以需要屏蔽combobox的上下键切换功能,并且加上文本框换行的功能combobox自身的previewkeydown事件是无法捕获到上下键的......
  • VAR:Visual Autoregressive Modeling: Scalable ImageGeneration via Next-Scale Pred
    目录一、概述二、相关工作1、大型自回归语言模型的性质2、视觉生成三、VAR 1、讨论传统AR的缺点2、VAR框架一、概述    该论文提出了一种新的生成范式VAR视觉自回归模型,(区别于ddpm那种加噪之后unet去噪的工作),这种自回归模型实现coarsetofine的方法进行生......
  • 【Python GUI 编程】tkinter :Ttk 组合框 Combobox
    在本文中,将介绍如何创建一个tkinterCombobox组合框小部件,该小部件允许用户从一组值中选择一个值。Combobox组合框小部件是新增的Ttk主题小部件,是Entry文本框和Listbox列表框的组合。除了允许在一组值中选择一个值外,它还允许输入自定义值。创建组合框要创建组合框小部......
  • WPF call graphic draw functions via bitmap converting to bitmapimage
    usingSystem;usingSystem.Collections.Generic;usingSystem.Drawing;usingSystem.IO;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Windows.Data;usingSystem.Windows......
  • EmbodiedAI具身智能必读论文|MP5: A Multi-modal Open-ended Embodied System in Mine
    论文标题MP5:AMulti-modalOpen-endedEmbodiedSysteminMinecraftviaActivePerception论文链接:MP5:AMulti-modalOpen-endedEmbodiedSysteminMinecraftviaActivePerception论文下载论文作者YiranQin,EnshenZhou,QichangLiu,ZhenfeiYin,LuSheng,......
  • Sigrity System Explorer ViaWizard模式进行过孔建模和仿真分析操作指导
    SigritySystemExplorerViaWizard模式进行过孔建模和仿真分析操作指导SigritySystemExplorerViaWizard模式可以用于过孔建模用于前仿真分析,建模好过孔进行3D全波仿真分析评估过孔结构的性能,同样以差分模板为例具体操作如下双击打开SystemExplorer软件界面打开如......
  • C#,.net 8 console call MessageBox of System.Windows.Forms, and via MessageBox of
    usingSystem.Runtime.InteropServices;namespaceConsoleApp6{internalclassProgram{//copyfrom,https://gist.github.com/6rube/34b561827f0805f73742541b8b8bb770[DllImport("user32.dll",CharSet=CharSet.Unicode)]......
  • 调用WebService异常:提供的 URI 方案“https”无效,应为“http”。 (Parameter 'via')
    解决办法:varbinding=newBasicHttpBinding();binding.Security.Mode=BasicHttpSecurityMode.Transport;<bindings><customBinding><bindingname="ZWS_CUST"><mtomMessageEncodingmes......
  • Topology-Driven Multi-View Clustering via Tensorial Refined Sigmoid Rank Minimiz
    Topology-DrivenMulti-ViewClusteringviaTensorialRefinedSigmoidRankMinimization翻译通过张量化改进的Sigmoid秩最小化实现的拓扑驱动多视图聚类ZhibinGuKDD2024北京交通大学冯松鹤通信作者PANDA模型从欧几里得图中提取拓扑结构,以有效捕捉数据点的相......
  • 8-bit Optimizers via Block-wise Quantization
    目录概8-bitOptimizersDettmersT.,LewisM.,ShleiferS.andZettlemoyerL.8-bitoptimizersviablock-wisequantization.ICLR,2022.概本文提出了一种8-bit的优化器,其主要贡献算是block-wise的量化(从我的角度看一点也不novel)?8-bitOptimizers对于......