wpf(5) Listbox的ItemsSource、binding、StaticResource、项目样式



image.png

xaml源码:


几个说明:

(1)命名空间

<Window ... >:
这是定义 WPF 窗口的根元素。

x:Class="WpfApp1.MainWindow":
这指示该 XAML 文件与名为 MainWindow 的类相关联,该类位于 WpfApp1 命名空间中。
这允许你在 C# 或其他 .NET 语言中编写与该窗口交互的代码。

xmlns 属性:
这些定义了不同的 XML 命名空间(Namespace),使得你可以使用来自这些命名空间的元素和属性。
xmlns="...":定义了默认的 XML 命名空间,用于 WPF 的核心元素。
xmlns:x="...":定义了 x 前缀的命名空间,用于 XAML 特定的元素和属性,如 x:Class。
xmlns:d="...":定义了 d 前缀的命名空间,该命名空间通常用于设计时的数据和属性,
    这些数据和属性在运行时会被忽略(由 mc:Ignorable="d" 指定)。
xmlns:mc="...":定义了 mc 前缀的命名空间,用于标记兼容性设置。
xmlns:local="clr-namespace:WpfApp1":定义了 local 前缀的命名空间,
     用于引用与 XAML 文件位于同一项目中的 CLR (Common Language Runtime) 命名空间。
xmlns:sys="clr-namespace:System;assembly=mscorlib":这定义了一个到 System 命名空间的引用,
     但通常你不需要在 WPF 的 XAML 中直接引用 mscorlib 除非有特定的需求。
mc:Ignorable="d":
这告诉 XAML 解析器忽略 d 命名空间中的所有元素和属性。
这允许你在设计时添加数据(如 Blend 使用的数据),而这些数据在编译和运行应用时不会被包含。


(2)DataContext

       设置数据上下文,如果没有这个设置,xaml这边就访问不到People 

(3)Window.Resources

       是定义资源。下面定义了两个资源。

    (1)字符串数组MyStrings,这个可以给Listbox的ItemSource

    (2)样式 MyListBoxItemStyle

 (4)模板与触发器

    控件模板

    样式包含一个 ControlTemplate,它定义了 ListBoxItem 的外观。

    两个 Border 元素:borderHeader 和 border

    <ContentPresenter>:显示 ListBoxItem 的内容。

  它绑定到 ListBoxItem 的 VerticalContentAlignment 和 HorizontalContentAlignment 属性,以正确地对齐内容。

        触发器

        IsSelected 触发器:当 ListBoxItem 被选中时触发。

        IsMouseOver 触发器:当鼠标悬停在 ListBoxItem 上时触发。

<Window x:Class="WpfApp1.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:WpfApp1"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
        <local:MainWindowViewModel/>
    </Window.DataContext>
    <Window.Resources>
        <x:Array Type="{x:Type sys:String}" x:Key="MyStrings">
            <sys:String>Item 1</sys:String>
            <sys:String>Item 2</sys:String>
            <sys:String>Item 3</sys:String>
        </x:Array>

        <Style x:Key="MyListBoxItemStyle" TargetType="ListBoxItem">
            <Setter Property="MinHeight" Value="40"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListBoxItem}">
                        <Grid>
                            <Border x:Name="borderHeader"/>
                            <Border x:Name="border"/>
                            <ContentPresenter VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                       HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                       />

                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected" Value="True">
                                <Setter Property="BorderThickness" TargetName="borderHeader" Value="4,0,0,0"/>
                                <Setter Property="BorderBrush" TargetName="borderHeader" Value="Blue"/>
                                <Setter Property="Background" TargetName="border" Value="Red"/>
                                <Setter Property="Opacity" TargetName="border" Value="0.2"/>
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Background" TargetName="border" Value="Yellow"/>
                                <Setter Property="Opacity" TargetName="border" Value="0.4"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>

                </Setter.Value>
            </Setter>
        </Style>

       
    </Window.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
        </Grid.RowDefinitions>

        <ListBox HorizontalContentAlignment="Stretch" 
             ItemContainerStyle="{StaticResource MyListBoxItemStyle}" 
               ItemsSource="{Binding People}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Background="Transparent" Orientation="Horizontal" Margin="5" >
                        <TextBlock Text="{Binding Name}" />
                        <TextBlock Text=","/>
                        <TextBlock Text="{Binding Age}"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
       
    </Grid>
</Window>


MainWindowViewModel.cs代码:


wpf中为啥要用这个ObservableCollection,不能是List<T>吗?

在WPF(Windows Presentation Foundation)中,ObservableCollection<T> 通常用于数据绑定,因为它实现了 INotifyCollectionChanged 接口,这意味着当集合中的项目添加、移除或替换时,它会触发一个事件。这个事件可以被数据绑定机制捕获,从而通知WPF UI(用户界面)更新以反映数据集合的更改。

而List<T>不实现INotifyCollectionChanged 接口。

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WpfApp1
{
    public class MainWindowViewModel
    {
        public ObservableCollection<Person> People { get; set; }

        public MainWindowViewModel()
        {
            People = new ObservableCollection<Person>
            {
                new Person { Name = "Alice", Age = 30 },
                new Person { Name = "Bob", Age = 25 },
                new Person { Name = "Charlie", Age = 35 }
            };
        }
    }

    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
}



源码:

链接:https://pan.baidu.com/s/1rbDLk0XgU9sGjLUliREQRw 

提取码:gj1d 

--来自百度网盘超级会员V6勇哥的分享


本文出自勇哥的网站《少有人走的路》wwww.skcircle.com,转载请注明出处!讨论可扫码加群:
本帖最后由 勇哥,很想停止 于 2024-06-17 22:33:36 编辑

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

会员中心
搜索
«    2025年4月    »
123456
78910111213
14151617181920
21222324252627
282930
网站分类
标签列表
最新留言
    热门文章 | 热评文章 | 随机文章
文章归档
友情链接
  • 订阅本站的 RSS 2.0 新闻聚合
  • 扫描加本站机器视觉QQ群,验证答案为:halcon勇哥的机器视觉
  • 点击查阅微信群二维码
  • 扫描加勇哥的非标自动化群,验证答案:C#/C++/VB勇哥的非标自动化群
  • 扫描加站长微信:站长微信:abc496103864
  • 扫描加站长QQ:
  • 扫描赞赏本站:
  • 留言板:

Powered By Z-BlogPHP 1.7.2

Copyright Your skcircle.com Rights Reserved.

鄂ICP备18008319号


站长QQ:496103864 微信:abc496103864