您好,
会员登录 快速注册
退出 ( 条未读消息)
关于本站 意见反馈 首页

公告:小宅博客网可以开发票了,需要发票的,去群里找群主哈!!
全部文章分类
  • 人工智能 >

  • 编程语言 >

  • WPF系列 >

  • ASP.NET系列 >

  • Linux >

  • 数据库 >

  • 嵌入式 >

  • WEB技术 >

  • PLC系列 >

  • 微服务与框架 >

  • 小宅DIY >

  • 学习资料 >

OpenCv基础 ANN车牌识别 yolov5车牌识别 指针式仪表识别 ROS系列 YOLO Halcon Detectron2 昇腾AI ChatGPT在线体验 英伟达JETSON ChatGLM ChatTTS FunASR 地平线 ByteTrack 魔搭社区 LangChain
C C# C++ Python Java Go
WPF
ASP.NET小功能 GPS定位系统-MVC GPS定位系统-VUE ASP.NET WebRTC
Linux Linux内核 Shell MakeFile
MySql SqlServer Oracle
STM8 STM32 51单片机
VUE入门 HTML JavaScript CSS layui镜像网站 ElementUi中文官网 element-plus 图标
三菱 欧姆龙 西门子 施耐德 松下 台达
IOTSharp IOTGateway ABP FRAMEWORK Docker
亚克力音响 编程仙途:智驭万法
面试题与技巧 Python入门技能树 微软C#教程
首页 编程之美 工具下载 全国就业 流量地图 文心一言
WPF
内容介绍 1、新建WPF工程 2、Application介绍 3、Dispatcher介绍 4、Window 介绍 5、主要布局属性介绍 6、Grid网格布局 7、UniformGrid布局 8、DockPanel与ViewBox布局 9、Border与ViewBox布局 10、依赖属性(一) 11、依赖属性(二) 12、依赖属性(三) 13、依赖属性(四) 14、WPF中的数据绑定(一) 15、WPF中的数据绑定(二) 16、WPF中的数据绑定(三) 17、WPF中的数据绑定(四) 18、ListView示例(一) 19、ListView示例(二) 20、DataGrid示例(一) 21、DataGrid示例(二) 22、DataGrid示例(三) 23、引用FontAwesome矢量图 24、ListBox日志效果 25、Polygon绘制多边形 26、Ellipse绘制实心圆 27、数据模板DataTemplate 基于WPF的exe远程升级程序
26、Ellipse绘制实心圆
基于WPF的exe远程升级程序
激萌の小宅 小宅博客网 WPF

文章作者:激萌の小宅

促销:¥0

价格:¥0

配送方式: 购买后立即生效(如购买异常,请联系站长)
付款之后一定要等待自动跳转结束,否则购买可能会失败
  • 0 天

    有效期

  • 0

    总销量

  • 3

    累计评价

数据模板DataTemplate - (二十七)


这里提供一种DataTemplate使用方法,还有如何获取ListView当前选中项:


运行效果如下:


MainWindow.xaml 代码如下:

<Window x:Class="WpfApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="350">
    <Grid>
        <!-- 分两列 -->
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="55" />
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <Grid Grid.Column="0" x:Name="column1"/>
        <Grid Grid.Column="1" x:Name="column2"/>
        <ListView Name="MyListView" BorderThickness="2" Grid.ColumnSpan="2" Background="Black" FontSize="18" >
            <ListView.View>
                <!-- AllowsColumnReorder:是否允许排序 -->
                <GridView AllowsColumnReorder="False">
                    <GridViewColumn Header="序号" Width="{Binding ElementName=column1,Path=ActualWidth}">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <!-- Ellipse 画圆 -->
                                    <Ellipse Name="ep" Width="40" Height="40" Fill="Gray" DataContext="111" />
                                    <!-- RowIndex 是序号 -->
                                    <TextBlock Grid.Column="0" Margin="-40,0,0,0" Foreground="Black" VerticalAlignment="Center" 
                                        HorizontalAlignment="Center" FontSize="18" Text="{Binding RowIndex }" ></TextBlock>
                                </StackPanel>

                                <!-- Status 是状态,根据不同的Value值设置圆(ep)的不同颜色 -->
                                <!-- DataTrigger 是数据触发器 -->
                                <DataTemplate.Triggers>
                                    <DataTrigger Binding="{Binding Status}" Value="0">
                                        <Setter Property="Fill" Value="blue" TargetName="ep"/>
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding Status}" Value="1">
                                        <Setter Property="Fill" Value="Red" TargetName="ep"/>
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding Status}" Value="2">
                                        <Setter Property="Fill" Value="Green" TargetName="ep"/>
                                    </DataTrigger>
                                </DataTemplate.Triggers>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="内容" Width="{Binding ElementName=column2,Path=ActualWidth}" x:Name="MyText">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Grid.Column="0" TextWrapping="Wrap"  Foreground="White" VerticalAlignment="Center" 
                                    HorizontalAlignment="Center" FontSize="18" Text="{Binding Text }" ></TextBlock>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</Window>


MainWindow.xaml.cs 代码如下:

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Threading;
using System.Windows;

namespace WpfApp
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        /// <summary>
        /// 信息
        /// </summary>
        public ObservableCollection<CurrenTrayInfoList> CurrenTrayInfoListOne { get; set; } = new ObservableCollection<CurrenTrayInfoList>();

        public MainWindow()
        {
            InitializeComponent();

            // 信息
            MyListView.ItemsSource = CurrenTrayInfoListOne;

            // 启动线程
            new Thread(ThreadData) { IsBackground = true }.Start();
        }
        private void ThreadData()
        {
            Thread.Sleep(3000);

            Random rd = new Random();
            for (int i = 1; i <= 5; i++)
            {
                CurrenTrayInfoList currentTrayEntity = new CurrenTrayInfoList();
                currentTrayEntity.RowIndex = i;
                currentTrayEntity.Text = $"时间:{DateTime.Now}";
                currentTrayEntity.Status = rd.Next(4);
                Dispatcher.BeginInvoke((Action)delegate ()
                {
                    CurrenTrayInfoListOne.Add(currentTrayEntity);
                });
                Thread.Sleep(1000);
            }

            // 获取选中状态
            while (true)
            {
                Dispatcher.BeginInvoke((Action)delegate ()
                {
                    CurrenTrayInfoList c = MyListView.SelectedItem as CurrenTrayInfoList;
                    if (c != null) MyText.Header = c.RowIndex + ":" + c.Text;
                });
                Thread.Sleep(100);
            }
        }
        public class CurrenTrayInfoList : ModelBase
        {
            private int _rowindex;
            private string _text;
            private int _status;

            /// <summary>
            /// 序号
            /// </summary> 
            public int RowIndex
            {
                set
                {
                    _rowindex = value;
                    this.OnPropertyChanged("RowIndex");
                }
                get { return _rowindex; }
            }

            /// <summary>
            /// Text
            /// </summary> 
            public string Text
            {
                set
                {
                    _text = value;
                    this.OnPropertyChanged("Text");
                }
                get { return _text; }
            }

            /// <summary>
            /// 状态
            /// </summary> 
            public int Status
            {
                set
                {
                    _status = value;
                    this.OnPropertyChanged("Status");
                }
                get { return _status; }
            }
        }

        [Serializable]
        public abstract class ModelBase : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;

            protected void OnPropertyChanged(string propertyName)
            {
                if (propertyName != null)
                {
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
                }
            }
        }
    }
}
26、Ellipse绘制实心圆
基于WPF的exe远程升级程序

友情链接: CSDN激萌の小宅 95知识库 自考题库 罗分明个人网络博客 精益编程leanboot

小宅博客  www.bilibili996.com All Rights Reserved. 备案号: 闽ICP备2024034575号

网站经营许可证  福建省福州市 Copyright©2021-2025 版权所有

小宅博客
首页 智能家居 地图定位
公告:小宅博客网可以开发票了,需要发票的,去群里找群主哈!!

文章作者:激萌の小宅

促销:¥0

价格:¥0

配送方式: 购买后立即生效(如购买异常,请联系站长)
付款之后一定要等待自动跳转结束,否则购买可能会失败
  • 0 天

    有效期

  • 0

    总销量

  • 3

    累计评价

数据模板DataTemplate - (二十七)


这里提供一种DataTemplate使用方法,还有如何获取ListView当前选中项:


运行效果如下:


MainWindow.xaml 代码如下:

<Window x:Class="WpfApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="350">
    <Grid>
        <!-- 分两列 -->
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="55" />
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <Grid Grid.Column="0" x:Name="column1"/>
        <Grid Grid.Column="1" x:Name="column2"/>
        <ListView Name="MyListView" BorderThickness="2" Grid.ColumnSpan="2" Background="Black" FontSize="18" >
            <ListView.View>
                <!-- AllowsColumnReorder:是否允许排序 -->
                <GridView AllowsColumnReorder="False">
                    <GridViewColumn Header="序号" Width="{Binding ElementName=column1,Path=ActualWidth}">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <!-- Ellipse 画圆 -->
                                    <Ellipse Name="ep" Width="40" Height="40" Fill="Gray" DataContext="111" />
                                    <!-- RowIndex 是序号 -->
                                    <TextBlock Grid.Column="0" Margin="-40,0,0,0" Foreground="Black" VerticalAlignment="Center" 
                                        HorizontalAlignment="Center" FontSize="18" Text="{Binding RowIndex }" ></TextBlock>
                                </StackPanel>

                                <!-- Status 是状态,根据不同的Value值设置圆(ep)的不同颜色 -->
                                <!-- DataTrigger 是数据触发器 -->
                                <DataTemplate.Triggers>
                                    <DataTrigger Binding="{Binding Status}" Value="0">
                                        <Setter Property="Fill" Value="blue" TargetName="ep"/>
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding Status}" Value="1">
                                        <Setter Property="Fill" Value="Red" TargetName="ep"/>
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding Status}" Value="2">
                                        <Setter Property="Fill" Value="Green" TargetName="ep"/>
                                    </DataTrigger>
                                </DataTemplate.Triggers>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="内容" Width="{Binding ElementName=column2,Path=ActualWidth}" x:Name="MyText">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Grid.Column="0" TextWrapping="Wrap"  Foreground="White" VerticalAlignment="Center" 
                                    HorizontalAlignment="Center" FontSize="18" Text="{Binding Text }" ></TextBlock>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</Window>


MainWindow.xaml.cs 代码如下:

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Threading;
using System.Windows;

namespace WpfApp
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        /// <summary>
        /// 信息
        /// </summary>
        public ObservableCollection<CurrenTrayInfoList> CurrenTrayInfoListOne { get; set; } = new ObservableCollection<CurrenTrayInfoList>();

        public MainWindow()
        {
            InitializeComponent();

            // 信息
            MyListView.ItemsSource = CurrenTrayInfoListOne;

            // 启动线程
            new Thread(ThreadData) { IsBackground = true }.Start();
        }
        private void ThreadData()
        {
            Thread.Sleep(3000);

            Random rd = new Random();
            for (int i = 1; i <= 5; i++)
            {
                CurrenTrayInfoList currentTrayEntity = new CurrenTrayInfoList();
                currentTrayEntity.RowIndex = i;
                currentTrayEntity.Text = $"时间:{DateTime.Now}";
                currentTrayEntity.Status = rd.Next(4);
                Dispatcher.BeginInvoke((Action)delegate ()
                {
                    CurrenTrayInfoListOne.Add(currentTrayEntity);
                });
                Thread.Sleep(1000);
            }

            // 获取选中状态
            while (true)
            {
                Dispatcher.BeginInvoke((Action)delegate ()
                {
                    CurrenTrayInfoList c = MyListView.SelectedItem as CurrenTrayInfoList;
                    if (c != null) MyText.Header = c.RowIndex + ":" + c.Text;
                });
                Thread.Sleep(100);
            }
        }
        public class CurrenTrayInfoList : ModelBase
        {
            private int _rowindex;
            private string _text;
            private int _status;

            /// <summary>
            /// 序号
            /// </summary> 
            public int RowIndex
            {
                set
                {
                    _rowindex = value;
                    this.OnPropertyChanged("RowIndex");
                }
                get { return _rowindex; }
            }

            /// <summary>
            /// Text
            /// </summary> 
            public string Text
            {
                set
                {
                    _text = value;
                    this.OnPropertyChanged("Text");
                }
                get { return _text; }
            }

            /// <summary>
            /// 状态
            /// </summary> 
            public int Status
            {
                set
                {
                    _status = value;
                    this.OnPropertyChanged("Status");
                }
                get { return _status; }
            }
        }

        [Serializable]
        public abstract class ModelBase : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;

            protected void OnPropertyChanged(string propertyName)
            {
                if (propertyName != null)
                {
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
                }
            }
        }
    }
}