首页 > 其他分享 >WPF ItemsControl.AlternationIndex AlternationCount

WPF ItemsControl.AlternationIndex AlternationCount

时间:2024-11-17 16:29:46浏览次数:1  
标签:AlternationIndex get Windows System set AlternationCount using WPF public

<Style TargetType="{x:Type Control}" x:Key="lbxStyle">
    <Style.Triggers>
        <Trigger Property="ItemsControl.AlternationIndex" Value="0">
            <Setter Property="Background" Value="Red"/>
        </Trigger>
        <Trigger Property="ItemsControl.AlternationIndex" Value="1">
            <Setter Property="Background" Value="Green"/>
        </Trigger>
        <Trigger Property="ItemsControl.AlternationIndex" Value="2">
            <Setter Property="Background" Value="Blue"/>
        </Trigger>
        <Trigger Property="ItemsControl.AlternationIndex" Value="3">
            <Setter Property="Background" Value="Yellow"/>
        </Trigger>
        <Trigger Property="ItemsControl.AlternationIndex" Value="4">
            <Setter Property="Background" Value="Purple"/>
        </Trigger>
    </Style.Triggers>
</Style>

 

 

 

 

 

//xaml
<Window x:Class="WpfApp35.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"
        WindowState="Maximized"
        xmlns:local="clr-namespace:WpfApp35"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <Style TargetType="{x:Type Control}" x:Key="lbxStyle">
            <Style.Triggers>
                <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                    <Setter Property="Background" Value="Red"/>
                </Trigger>
                <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                    <Setter Property="Background" Value="Green"/>
                </Trigger>
                <Trigger Property="ItemsControl.AlternationIndex" Value="2">
                    <Setter Property="Background" Value="Blue"/>
                </Trigger>
                <Trigger Property="ItemsControl.AlternationIndex" Value="3">
                    <Setter Property="Background" Value="Yellow"/>
                </Trigger>
                <Trigger Property="ItemsControl.AlternationIndex" Value="4">
                    <Setter Property="Background" Value="Purple"/>
                </Trigger>
            </Style.Triggers>
        </Style>
        <Style TargetType="{x:Type TextBlock}" x:Key="tbkStyle">
            <Setter Property="Width" Value="300"/>
            <Setter Property="Height" Value="100"/>
            <Setter Property="FontSize" Value="30"/>
            <Setter Property="Foreground" Value="Black"/>
            <Setter Property="HorizontalAlignment" Value="Stretch"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="FontSize" Value="50"/>
                    <Setter Property="Foreground" Value="Red"/>
                    <Setter Property="FontWeight" Value="ExtraBold"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <ListBox ItemsSource="{Binding BooksCollection,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                 AlternationCount="5"
                 ItemContainerStyle="{StaticResource lbxStyle}"                 
                 VirtualizingPanel.IsContainerVirtualizable="True"
                 VirtualizingPanel.IsVirtualizing="True"
                 VirtualizingPanel.CacheLength="1"
                 VirtualizingPanel.CacheLengthUnit="Item">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Border Width="100" Height="100">
                            <Border.Background>
                                <ImageBrush ImageSource="{Binding ImgSource}" 
                                            Stretch="Uniform"
                                            RenderOptions.BitmapScalingMode="Fant"/>
                            </Border.Background>
                        </Border>
                        <TextBlock Text="{Binding Id}" Style="{StaticResource tbkStyle}"/>
                        <TextBlock Text="{Binding ISBN}" Style="{StaticResource tbkStyle}"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>


//xaml.cs
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
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;

namespace WpfApp35
{
    /// <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())
            {
                imgsList = new List<string>(imgs);
                imgsCount = imgsList.Count;
                BooksCollection = new ObservableCollection<Book>();
                for (int i = 0; i < imgsCount; i++)
                {
                    BooksCollection.Add(new Book()
                    {
                        Id = i + 1,
                        ISBN = $"ISBN_{i + 1}",
                        ImgUrl = imgs[i],
                        ImgSource = GetImageSourceViaUrl(imgs[i])
                    });
                }
            }
        }

        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(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }

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

        private List<string> imgsList { get; set; }
        private int imgsCount { get; set; }
    }

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

 

标签:AlternationIndex,get,Windows,System,set,AlternationCount,using,WPF,public
From: https://www.cnblogs.com/Fred1987/p/18550710

相关文章

  • WPF Static ToolBar.ButtonStyleKey
    <Windowx:Class="WpfApp33.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 style BasedOn base style
    <Windowx:Class="WpfApp32.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如何全局应用黑白主题效果
    灰白色很多时候用于纪念,哀悼等。那么使用WPF如何来做到这种效果呢?要实现的这种效果,我们会发现,它其实不仅仅是要针对图片,而是要针对整个窗口来实现灰白色。如果只是针对图片的话,我可以可以对图片进行灰阶转换,即可达到灰色效果。以下是图片转灰阶的代码,当然方法不仅仅是这一种......
  • WPF Prism框架
    一、关于Prism框架Prism.Core:【Prism.dll】实现MVVM的核心功能,属于一个与平台无关的项目Prism.Wpf:【Prism.Wpf】包含了DialogService,Region,Module,Navigation,其他的一些WPF的功能Prism.Unity:【Prism.Unity.Wpf】,IOC容器Prism.Unity=>Prism.Wpf=>Prism.Core二、Pri......
  • 详解WPF中的MVVM模式(二)
    文章目录1.视图模型优先介绍2.视图模型优先实现2.1ContentControl2.2实现代码3.视图模型优先示例4.总结继续接着上篇讲解WPF中的MVVM模式,本文主要讲解的是视图模型(ViewModelFirst)优先的实现方式。1.视图模型优先介绍在上篇文章中我们讲到,视图优先(ViewFirst)就......
  • WPF 打开资源管理器且选中某个文件
    打开资源管理器且选中某个文件可以使用cmd调用explorer带上select参数,如下面命令行所示explorer.exe/select,"C:\Folder\file.txt"但有很多情况下,用户可能使用其他资源管理器,此时将会导致应用软件打开的是explorer而不是用户默认的资源管理器通过shell32.dll提供的......
  • 【WPF】Prism学习(二)
    PrismCommands1.命令(Commanding)1.1.ViewModel的作用:ViewModel不仅提供在视图中显示或编辑的数据,还可能定义一个或多个用户可以执行的动作或操作。这些用户可以通过用户界面(UI)执行的动作或操作通常被定义为命令(Commands)。1.2.命令(Commands)的作用:命令提供了一种方便......
  • 界面控件DevExpress WPF中文教程:TreeList视图及创建分配视图
    DevExpressWPF拥有120+个控件和库,将帮助您交付满足甚至超出企业需求的高性能业务应用程序。通过DevExpressWPF能创建有着强大互动功能的XAML基础应用程序,这些应用程序专注于当代客户的需求和构建未来新一代支持触摸的解决方案。无论是Office办公软件的衍伸产品,还是以数据为中心......
  • wpf tabitem 横向分布
    <Window.Resources><Stylex:Key="newTabControl"TargetType="TabControl"><SetterProperty="Template"><Setter.Value><ControlTemplateTargetType="{x:TypeTab......
  • tips1:WPF绑定的一种情况 Binding
    <CheckBoxMargin="10"VerticalAlignment="Center"IsChecked="{BindingRelativeSource={RelativeSourceTemplatedparent},Path=IsExpanded}"/>CheckBox的IsChecked属性使用了数据绑定机制,以实现与TemplatedParent......