WPF ListBox的進階使用(一)

来源:https://www.cnblogs.com/Johar/archive/2018/08/30/9562187.html
-Advertisement-
Play Games

公司項目有個需求,UI界面支持動態平均分割界面,想了想便想到用ListBox來實現,用UniformGrid作為ListBox的ItemsPanelTemplate,通過動態改變UniformGrid的Columns屬性,可以動態分割界面。具體實現如下所示: 對應的ViewModel層代碼: 軟體運 ...


公司項目有個需求,UI界面支持動態平均分割界面,想了想便想到用ListBox來實現,用UniformGrid作為ListBox的ItemsPanelTemplate,通過動態改變UniformGrid的Columns屬性,可以動態分割界面。具體實現如下所示:

 1 <Window x:Class="WpfDemo.MainWindow"
 2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4         xmlns:comm="clr-namespace:WpfDemo.CommonControls;assembly=WpfDemo.CommonControls"
 5         xmlns:local="clr-namespace:WpfDemo"
 6         Title="MainWindow" Height="350" Width="525">
 7     
 8     <Grid>
 9         <ListBox ItemsSource="{Binding DataSource}">
10             <ListBox.ItemsPanel>
11                 <ItemsPanelTemplate>
12                     <UniformGrid Columns="{Binding Colums}" IsItemsHost="True"/>
13                 </ItemsPanelTemplate>
14             </ListBox.ItemsPanel>
15             <ListBox.ItemContainerStyle>
16                 <Style TargetType="{x:Type ListBoxItem}">
17                     <Setter Property="Template">
18                         <Setter.Value>
19                             <ControlTemplate TargetType="{x:Type ListBoxItem}">
20                                 <Border HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Green" BorderBrush="Yellow" BorderThickness="1">
21                                     <TextBlock Text="{Binding CameraName}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
22                                 </Border>
23                             </ControlTemplate>
24                         </Setter.Value>
25                     </Setter>
26                 </Style>
27             </ListBox.ItemContainerStyle>
28         </ListBox>
29     </Grid>
30 </Window>

對應的ViewModel層代碼:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Linq;
 5 using System.Runtime.CompilerServices;
 6 using System.Text;
 7 using System.Threading.Tasks;
 8 
 9 namespace WpfDemo
10 {
11     public abstract class NotifyPropertyBase : INotifyPropertyChanged
12     {
13         protected void SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
14         {
15             if (object.Equals(storage, value)) return;
16             storage = value;
17             this.OnPropertyChanged(propertyName);
18         }
19 
20         protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
21         {
22             if (this.PropertyChanged != null)
23             {
24                 this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
25             }
26         }
27 
28         public event PropertyChangedEventHandler PropertyChanged;
29     }
30 }
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Collections.ObjectModel;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 using System.Windows.Threading;
 8 
 9 namespace WpfDemo
10 {
11     public class MainWindowVM : NotifyPropertyBase
12     {
13         private DispatcherTimer timer;
14         public MainWindowVM()
15         {
16             DataSource = new ObservableCollection<WndViewModel>();
17             Colums = 1;
18             timer = new DispatcherTimer();
19             timer.Interval = new TimeSpan(0, 0, 1);
20             timer.Tick += timer_Tick;
21             timer.Start();
22         }
23 
24         private int count = 0;
25         void timer_Tick(object sender, EventArgs e)
26         {
27             var temp = new WndViewModel()
28             {
29                 CameraName = string.Format("Camera {0}", ++count),
30             };
31             DataSource.Add(temp);
32             Console.WriteLine(temp.CameraName);
33             if (count <= 6)
34             {
35                 Colums = count;
36             }
37             else if (count > 100)
38             {
39                 count = 0;
40                 DataSource.Clear();
41                 Colums = 1;
42             }
43         }
44 
45         private int colums;
46         public int Colums
47         {
48             get { return colums; }
49             set
50             {
51                 SetProperty(ref colums, value);
52             }
53         }
54 
55         private ObservableCollection<WndViewModel> dataSource;
56         public ObservableCollection<WndViewModel> DataSource
57         {
58             get { return dataSource; }
59             set
60             {
61                 SetProperty(ref dataSource, value);
62             }
63         }
64     }
65 }

軟體運行的效果:

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 1、環境簡介Visual Studio 2013社區版Halcon18.052、使用Nuget在VS工程中安裝Halcon插件搜索欄輸入關鍵字halcon,出現兩個插件,分別是halcon語言的介面和halcon引擎,全部安裝即可。需要註意的是,這兩個控制項的使用的前提是你的電腦上已經安裝了Halco... ...
  • 去年就開始學習採用Docker+Jenkins+.Net Core搭建生成式流水線,一直拖到現在,也沒有徹底的好好靜下來去總結總結。趁著現在對自己的嚴格要求下,逐漸開始重視自我總結,以此來鞏固逐漸失去的知識。 本文地址:https://www.cnblogs.com/CKExp/p/9536864. ...
  • 首先瞭解一下 RFC4646 和 BCP 47 是什麼東西: "RFC4646" The name is a combination of an ISO 639 two letter lowercase culture code associated with a language and an I ...
  • 背景 由於最近公司要做微信小程式聊天,所以.NetFramwork版本的SignalR版本的不能用了。因為小程式里沒有windows對象,導致JQuery無法使用。而Signalr的 js客戶端是依賴JQuery的。 所以看下了Core版本的SignarlR,經過測試,發現可以在微信中運行,不過要將 ...
  • Ocelot是為.net core量身定做的,目前是基於 netstandard2.0進行構建的。 .NET Core 2.1中如何使用呢? 安裝NuGet package 使用nuget安裝Ocelot及其依賴項。您需要創建一個netstandard2.0項目並將其Package安裝到項目中。然後 ...
  • App.xaml.cs中的代碼每次都差不多,故特地將其整理出來直接復用: ...
  • 問題: 需要讓程式(以非同步方式)等待一段時間。 解決方案:Task類的靜態函數Delay,返回Task對象 在github開源項目 ,找到Task.cs有關Delay方法的源碼 github地址: "https://github.com/dotnet/coreclr/blob/master/src/ ...
  • 項目中經常使用需要根據搜索條件查詢數據,然後用卡片來展示數據。用卡片展示數據時,界面的寬度發生變化,希望顯示的卡片數量也跟隨變化。WrapPanel雖然也可以實現這個功能,但是將多餘的部分都留在行尾,十分不美觀,最好是能夠將多餘的寬度平分在每個ListBoxItem之間,比較美觀,也符合項目需求。如 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...