WPF入門教程系列二十六——DataGrid使用示例(3)

来源:https://www.cnblogs.com/chillsrc/archive/2023/05/28/17438067.html
-Advertisement-
Play Games

# Unity IPostBuildPlayerScriptDLLs Unity IPostBuildPlayerScriptDLLs是Unity引擎中的一個非常有用的功能,它可以讓開發者在構建項目後自定義哪些文件需要被覆制到輸出目錄中。這個功能可以幫助開發者更好地控制項目的構建過程,確保輸出目錄只 ...


WPF入門教程系列目錄 WPF入門教程系列二——Application介紹

WPF入門教程系列三——Application介紹(續)

WPF入門教程系列四——Dispatcher介紹

WPF入門教程系列五——Window 介紹

WPF入門教程系列十一——依賴屬性(一) WPF入門教程系列十五——WPF中的數據綁定(一)

 

五、DataGrid的DataGridComboBoxColumn列的綁定方式

  在上一篇文章的示例中,存在一個問題,在點擊“刷新”按鈕之後,城市這個ComboBox列的數據沒有顯示。

DataGridComboBoxColumn列如果要填充數據,首先要設置列的ItemsSouce屬性,而且這個屬性對於要綁定的數據源有以下的要求:

  • 1、靜態資源。有關更多信息,請參見 StaticResource 標記擴展。
  • 2、x: 靜態代碼實體。有關更多信息,請參見 x:Static 標記擴展。
  • 3、ComboBoxItem 類型的內聯集合。

1.  在使用DataGrid的時候,有時候需要使某些列為ComboBox,這時自然想到使用DataGridComboBoxColumn,但是如果使用的是ItemsSource數據綁定後臺的對象,就會發現,這根本就不能用。

2.在Visual Studio 2022中打開MainWindow.cs文件,添加下拉框的綁定代碼。具體代碼如下:

   private void BindDrp()
        {
            cboCity.ItemsSource=GetCitys();
        }

        private void btnRefresh_Click(object sender, RoutedEventArgs e)
        {

            BindGrid();
            BindDrp();
        } 

3. 在Visual Studio 2022中打開MainWindow.xaml文件,對DataGridComboBoxColumn進行了數據綁定。具體代碼如下。

<DataGridComboBoxColumn Header="城市" Width="120"  x:Name="cboCity" ClipboardContentBinding="{x:Null}"
SelectedValuePath="Code" SelectedValueBinding="{Binding Path=CityCode,UpdateSourceTrigger=PropertyChanged}"
DisplayMemberPath
="Name" SelectedItemBinding="{x:Null}" />

       其中SelectedValuePath與DisplayMemberPath是指將綁定到DataGridComboBoxColumn列上的類中的哪個屬性。例如上面的代碼,code屬性做為value值,Name屬性做為在界面上呈現。

      通過SelectedValueBinding綁定的值,這個值由SelectedValuePath 所綁定的屬性確定,呈現綁定對象上的屬性。

      例如上述代碼中,將SelectedValueBinding綁定到CityCode,當屬性綁定發生變化時。通過SelectedValuePath=Code,表示我們通過Code這個關鍵字進行搜索,搜索綁定對象的Code值是否與CityCode值相同,相同返回City對象,而City對象中有Code屬性和Name屬性,並以Name屬性進行顯示。

4. 在Visual Studio 2022中按F5鍵,運行WFP應用程式,使用滑鼠左鍵,點擊“刷新”按鈕,DataGrid中的城市列預設顯示正確的綁定數據,如下圖。

 

5. 下麵是全部完成之後的實際的XAML代碼。

<Window x:Class="WpfGridDemo.NET7.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:WpfGridDemo.NET7"
        mc:Ignorable="d"
        Title="MainWindow" Height="600" Width="960">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="100"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>

            <RowDefinition Height="25"></RowDefinition>
        </Grid.RowDefinitions>

        <DataGrid x:Name="gridArea" Grid.Row="1" d:ItemsSource="{d:SampleData ItemCount=5}" AutoGenerateColumns="False"
HorizontalAlignment="Left" VerticalAlignment="Top"> <DataGrid.Columns> <DataGridComboBoxColumn Header="城市" Width="120" x:Name="cboCity" ClipboardContentBinding="{x:Null}"
SelectedValuePath
="Code" SelectedValueBinding="{Binding Path=CityCode,UpdateSourceTrigger=PropertyChanged}"
DisplayMemberPath
="Name" SelectedItemBinding="{x:Null}" /> <DataGridTextColumn Header="縣區鎮" Width="*" Binding="{Binding Name}" ClipboardContentBinding="{x:Null}"/> <DataGridTextColumn Header="郵編" Width="100" Binding="{Binding Code}" ClipboardContentBinding="{x:Null}"/> <DataGridTextColumn Header="創建時間" Width="160" Binding="{Binding Created}" ClipboardContentBinding="{x:Null}"/> <DataGridTextColumn Header="更新時間" Width="160" Binding="{Binding Updated}" ClipboardContentBinding="{x:Null}"/> </DataGrid.Columns> </DataGrid> <WrapPanel Grid.Row="2"> <Button x:Name="btnRefresh" Height="22" Width="120" Click="btnRefresh_Click">刷新</Button> <Button x:Name="btnSave" Height="22" Width="120">保存</Button> </WrapPanel> </Grid> </Window>

6.MainWidow.cs的全部代碼,如下

using System;
using System.Collections.Generic;
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;
using WpfGridDemo.NET7.Entitys;
 
namespace WpfGridDemo.NET7
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        GridDbContext db = new GridDbContext();

        protected List<City> GetCitys()
        {
            List<City> list = db.City.ToList<City>();

            return list;
 
        }
 
 
        protected List<Area> GetAreas()
        {
            List<Area> list = db.Area.ToList<Area>();
            return list;
        }
 
        protected List<Province> GetProvinces()
        {
            List<Province> list = db.Province.ToList<Province>();

            return list;

        }

        private void BindGrid()
        {
            gridArea.ItemsSource = GetAreas();
        }

        private void BindDrp()
        {
            cboCity.ItemsSource=GetCitys();
        }

        private void btnRefresh_Click(object sender, RoutedEventArgs e)
        {

            BindGrid();
            BindDrp();
        }
    }
}

 


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

-Advertisement-
Play Games
更多相關文章
  • 該模塊提供將二進位數據編碼為可列印ASCII字元並將這種編碼解碼回二進位數據的功能。它為[RFC 3548](https://tools.ietf.org/html/rfc3548.html)中指定的編碼提供編碼和解碼功能。定義了Base16、Base32和Base64演算法,以及事實上的標準Asci ...
  • # 關於STL容器的簡單總結 ## 1、結構體中重載運算符的示例 ``` //結構體小於符號的重載 struct buf { int a,b; bool operator queuea; //定義 a.push(x); //壓入 a.pop(); //彈出 a.size(); //取大小 a.fro ...
  • ### 超聲波模塊介紹 超聲波測距原理很簡單: 1、通過記錄發送超聲波的時間、記錄超聲波返回的時間,返回時間與發送時間相減得到超聲波的持續時間。 2、通過公式:(**超聲波持續時間** * **聲波速度**) / **2**就可以得出距離; ![image.png](https://img2023. ...
  • # Unity Undo詳解 在Unity中,Undo是一個非常重要的功能,它可以讓開發者在編輯器中進行操作時,隨時撤銷之前的操作,從而避免不必要的錯誤。本文將詳細介紹Unity Undo實現原理和使用方法,並提供多個使用例子,幫助開發者更好地理解和應用該功能。 ## 實現原理Unity Undo的 ...
  • 本篇博文對接上篇末尾處WPF常用佈局控制項的綜合應用,為[痕跡g](https://www.cnblogs.com/zh7791/p/11369020.html)佈局控制項介紹課後作業的一種思路方法。 ...
  • # Unity IPostGenerateGradleAndroidProject Unity是一款流行的跨平臺游戲引擎,它支持多種平臺,包括Android。在Unity中,我們可以使用IPostGenerateGradleAndroidProject介面來自定義Gradle構建過程。本文將介紹如何 ...
  • # Unity IGenerateNativePluginsForAssemblies Unity是一款非常流行的游戲引擎,它支持多種平臺,包括Windows、Mac、Linux、Android、iOS等。在Unity中,我們可以使用C#編寫游邏輯,但是有些時候我們需要使用一些原生的代碼來實現一些高 ...
  • # Unity IActiveBuildTargetChanged Unity IActiveBuildTargetChanged是Unity引擎中的一個非常有用的功能,它可以讓開發者在切換構建平臺時自定義哪些操作需要被執行。這個功能可以幫助開發者更好地控制項目的構建過程,確保在切換構建平臺時執行必 ...
一周排行
    -Advertisement-
    Play Games
  • Dapr Outbox 是1.12中的功能。 本文只介紹Dapr Outbox 執行流程,Dapr Outbox基本用法請閱讀官方文檔 。本文中appID=order-processor,topic=orders 本文前提知識:熟悉Dapr狀態管理、Dapr發佈訂閱和Outbox 模式。 Outbo ...
  • 引言 在前幾章我們深度講解了單元測試和集成測試的基礎知識,這一章我們來講解一下代碼覆蓋率,代碼覆蓋率是單元測試運行的度量值,覆蓋率通常以百分比表示,用於衡量代碼被測試覆蓋的程度,幫助開發人員評估測試用例的質量和代碼的健壯性。常見的覆蓋率包括語句覆蓋率(Line Coverage)、分支覆蓋率(Bra ...
  • 前言 本文介紹瞭如何使用S7.NET庫實現對西門子PLC DB塊數據的讀寫,記錄了使用電腦模擬,模擬PLC,自至完成測試的詳細流程,並重點介紹了在這個過程中的易錯點,供參考。 用到的軟體: 1.Windows環境下鏈路層網路訪問的行業標準工具(WinPcap_4_1_3.exe)下載鏈接:http ...
  • 從依賴倒置原則(Dependency Inversion Principle, DIP)到控制反轉(Inversion of Control, IoC)再到依賴註入(Dependency Injection, DI)的演進過程,我們可以理解為一種逐步抽象和解耦的設計思想。這種思想在C#等面向對象的編 ...
  • 關於Python中的私有屬性和私有方法 Python對於類的成員沒有嚴格的訪問控制限制,這與其他面相對對象語言有區別。關於私有屬性和私有方法,有如下要點: 1、通常我們約定,兩個下劃線開頭的屬性是私有的(private)。其他為公共的(public); 2、類內部可以訪問私有屬性(方法); 3、類外 ...
  • C++ 訪問說明符 訪問說明符是 C++ 中控制類成員(屬性和方法)可訪問性的關鍵字。它們用於封裝類數據並保護其免受意外修改或濫用。 三種訪問說明符: public:允許從類外部的任何地方訪問成員。 private:僅允許在類內部訪問成員。 protected:允許在類內部及其派生類中訪問成員。 示 ...
  • 寫這個隨筆說一下C++的static_cast和dynamic_cast用在子類與父類的指針轉換時的一些事宜。首先,【static_cast,dynamic_cast】【父類指針,子類指針】,兩兩一組,共有4種組合:用 static_cast 父類轉子類、用 static_cast 子類轉父類、使用 ...
  • /******************************************************************************************************** * * * 設計雙向鏈表的介面 * * * * Copyright (c) 2023-2 ...
  • 相信接觸過spring做開發的小伙伴們一定使用過@ComponentScan註解 @ComponentScan("com.wangm.lifecycle") public class AppConfig { } @ComponentScan指定basePackage,將包下的類按照一定規則註冊成Be ...
  • 操作系統 :CentOS 7.6_x64 opensips版本: 2.4.9 python版本:2.7.5 python作為腳本語言,使用起來很方便,查了下opensips的文檔,支持使用python腳本寫邏輯代碼。今天整理下CentOS7環境下opensips2.4.9的python模塊筆記及使用 ...