国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

WPF入門教程系列二十八 ——DataGrid使用示例MVVM模式(6)

這篇具有很好參考價(jià)值的文章主要介紹了WPF入門教程系列二十八 ——DataGrid使用示例MVVM模式(6)。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

WPF入門教程系列目錄
WPF入門教程系列二——Application介紹
WPF入門教程系列三——Application介紹(續(xù))
WPF入門教程系列四——Dispatcher介紹

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

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

八、在Command中傳遞參數(shù)

7.上面Buttom的Command類就是純命令,什么參數(shù)都不接收,這次的ProvinceChangedCommand類在執(zhí)行命令的時(shí)候,能夠傳參數(shù)!采用泛型的形式,給Action添加泛型參數(shù)。

8. 在Visual Studio 2022的解決方案資源管理器中,使用鼠標(biāo)右鍵單擊“Command”文件夾,在彈出菜單中選擇“添加--> 類”,在彈出的“添加新項(xiàng)”對(duì)話框中,選擇添加 “ProvinceChangedCommand”類,這是一個(gè)我們要實(shí)現(xiàn)的保存操作指令,然后選擇“添加”。ProvinceChangedCommand的具體代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Threading.Tasks;
using System.Windows.Input;
 
namespace WpfGridDemo.NET7.Command
{
    public class ProvinceChangedCommand<T> : ICommand
    {
        /// <summary>
        /// 命令能否執(zhí)行
        /// </summary>
        readonly Func<bool> _canExecute;
        /// <summary>
        /// 命令執(zhí)行的方法
        /// </summary>
        readonly Action<T> _execute;
 
        /// <summary>
        /// 命令的構(gòu)造函數(shù)
        /// </summary>
        /// <param name="action">命令需執(zhí)行的方法</param>
        /// <param name="canExecute">命令是否可以執(zhí)行的方法</param>
        public ProvinceChangedCommand(Action<T> action, Func<bool> canExecute)
        {
            _execute = action;
            _canExecute = canExecute;
        }
 
        /// <summary>
        /// 判斷命令是否可以執(zhí)行
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public bool CanExecute(Object parameter)
        {
            if (_canExecute == null)
                return true;
            return _canExecute();
        }
 
        /// <summary>
        /// 執(zhí)行命令
        /// </summary>
        /// <param name="parameter"></param>
        public void Execute(Object parameter)
        {
            _execute((T)parameter);
        }
 
        /// <summary>
        /// 事件追加、移除
        /// </summary>
        public event EventHandler CanExecuteChanged
        {
            add
            {
                if (_canExecute != null)
                    CommandManager.RequerySuggested += value;
            }
            remove
            {
                if (_canExecute != null)
                    CommandManager.RequerySuggested -= value;
            }
        }
 
    }
}
void ProviceSelectionChangedExecute(object sender)
        {
            try
            {
                if (sender is ComboBox)
                {
                    ComboBox drp=sender as ComboBox;
                    ProvinceCode=drp.SelectedValue.ToString();
                    GridDbContext db = new GridDbContext();
                    var list = db.City.AsTracking().ToList();
                    List<City> citys = list.Where(x => x.ProvinceCode == ProvinceCode).ToList();
                    cityList = new ObservableCollection<City>();
                    if (citys != null)
                    {
                        citys.ForEach((t) =>
 
                        { cityList.Add(t); }
                        );
                    }
 
                    var cityCodes = from city in citys
                                    select city.Code;
                    List<Area> areas = db.Area.AsTracking().ToList().Where(
x => cityCodes.Contains(x.CityCode)).ToList(); areaList = new ObservableCollection<Area>(); if (areas!=null) { areas.ForEach((t) => { areaList.Add(t); } ); } } } catch (Exception ex) { throw ex; } }

結(jié)果如圖:我們看到了省份下拉框中已經(jīng)了省份信息。

WPF入門教程系列二十八 ——DataGrid使用示例MVVM模式(6)

?文章來源地址http://www.zghlxwxcb.cn/news/detail-488528.html

9.通過綁定依賴屬性,實(shí)現(xiàn)自動(dòng)刷新需要實(shí)現(xiàn)以下三步:

1.Model繼承并實(shí)現(xiàn) INotifyPropertyChanged 接口;

2.數(shù)據(jù)集合使用ObservableCollection<T>集合;

3.View使用Binding數(shù)據(jù)對(duì)象屬性;

如果不行再看看集合在賦值前需要實(shí)例化,不然就出不來(必須要同一個(gè)源才行)

10. 在Visual Studio 2022中打開MainWindows.xmal文件,并將文件中的代碼修改成如下:

<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:be="http://schemas.microsoft.com/xaml/behaviors"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfGridDemo.NET7"
        mc:Ignorable="d"
        Title="MainWindow" Height="600" Width="960" Loaded="Window_Loaded" >
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="100"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="25"></RowDefinition>
        </Grid.RowDefinitions>
        <WrapPanel Grid.Row="0" HorizontalAlignment="Left">
            <ComboBox x:Name="cboProvince" DisplayMemberPath="Name" SelectedValuePath="Code" >
 
                <be:Interaction.Triggers>

                    <be:EventTrigger EventName="SelectionChanged">
                        <be:InvokeCommandAction Command="{Binding ProviceChangedAction}" 
CommandParameter
="{Binding ElementName=cboProvince}"/> </be:EventTrigger> </be:Interaction.Triggers> </ComboBox> </WrapPanel> <DataGrid x:Name="gridArea" Grid.Row="1" ItemsSource="{Binding GridAreaList}"
AutoGenerateColumns
="False" HorizontalAlignment="Left" VerticalAlignment="Top"
SelectedItem
="{Binding Path=AreaVM, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"> <DataGrid.Columns> <DataGridComboBoxColumn Header="城市" Width="120"
ItemsSource
="{Binding Path=DataContext.GridCityList,
RelativeSource={RelativeSource AncestorType={x:Type Window}}}
"
x:Name
="cboCity" ClipboardContentBinding="{x:Null}"
SelectedValuePath="Code" SelectedValueBinding="{Binding Path=CityCode,
UpdateSourceTrigger=PropertyChanged}
"
DisplayMemberPath
="Name" SelectedItemBinding="{x:Null}" /> <DataGridTextColumn Header="縣區(qū)鎮(zhèn)" Width="*" Binding="{Binding Name}"
ClipboardContentBinding
="{x:Null}"/> <DataGridTextColumn Header="郵編" Width="100" Binding="{Binding Code}"
ClipboardContentBinding
="{x:Null}"/> <DataGridTextColumn Header="創(chuàng)建時(shí)間" Width="160" Binding="{Binding Created}"
ClipboardContentBinding
="{x:Null}"/> <DataGridTextColumn Header="更新時(shí)間" 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" Command="{Binding ClickSaveAction}" >保存</Button> </WrapPanel> </Grid> </Window>

?

11. 在Visual Studio 2022中打開MainWindowsVM.cs文件,實(shí)現(xiàn)下拉框的選擇事件的Command命令綁定,將通過Command參數(shù)傳遞過來的省份信息,用于數(shù)據(jù)查詢,同時(shí)通知UI界面進(jìn)行數(shù)據(jù)刷新。具體如下代碼:
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.DirectoryServices.ActiveDirectory;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Input;
using WpfGridDemo.NET7.Entitys;
 
namespace WpfGridDemo.NET7.ViewModel
{
    public class MainWindowVM: ViewModelBase
    {
        public MainWindowVM() {
            cityList = new ObservableCollection<City>();
            areaList = new ObservableCollection<Area>();
        }
        private Area m_Area;
        /// <summary>
        /// 縣鎮(zhèn)區(qū)數(shù)據(jù)
        /// </summary>
        public Area AreaVM
        {
            get { return m_Area; }
            set { m_Area = value; }
        }
        private string m_Province_Code;
        /// <summary>
        /// 省--代碼
        /// </summary>
        public string ProvinceCode { get => m_Province_Code; set => m_Province_Code = value; }
        private ObservableCollection<Area> areaList;
 
         public ObservableCollection<Area> GridAreaList
         {
             get { return areaList; }
             set
             {
                areaList = value;
                 RaisePropertyChanged("GridAreaList");
             }
        }
        private ObservableCollection<City> cityList;
 
        public ObservableCollection<City> GridCityList
        {
            get { return cityList; }
            set
            {
                cityList = value;
                RaisePropertyChanged("GridCityList");
            }
        }
    
        /// <summary>
        /// 命令要執(zhí)行的方法
        /// </summary>
        void SaveExecute()
        {
            try
 
            {
                GridDbContext db = new GridDbContext();
                var list=db.Area.AsTracking().ToList();
                Area modifyArea = list.Where(x=>x.Id==AreaVM.Id).FirstOrDefault();
                if (modifyArea != null)
                {
                    modifyArea.Name = AreaVM.Name;
                    modifyArea.Updated = DateTime.Now;
                    db.SaveChanges();
                }
 
            }
            catch (Exception ex)
            {
 
                throw ex;
            }
        }
 
        /// <summary>
        /// 命令是否可以執(zhí)行
        /// </summary>
        /// <returns></returns>
        bool CanSaveExecute()
        {
            return false;
        }
 
        /// <summary>
        /// 創(chuàng)建新命令
        /// </summary>
        public ICommand ClickSaveAction
        {
            get
            {
                return new Command.SaveCommand(SaveExecute, CanSaveExecute);
            }
        }
        //combobox
        /// <summary>
        /// 命令要執(zhí)行的方法
        /// </summary>
        void ProviceSelectionChangedExecute(object sender)
        {
            try
 
            {
                if (sender is ComboBox)
                {
                    ComboBox drp=sender as ComboBox;
                    ProvinceCode=drp.SelectedValue.ToString();
                    GridDbContext db = new GridDbContext();
                    var list = db.City.AsTracking().ToList();

                    List<City> citys = list.Where(x => x.ProvinceCode == ProvinceCode).ToList();
                    var cityCodes = from city in citys
                                    select city.Code;
                    List<Area> areas = db.Area.AsTracking().ToList().Where(
x => cityCodes.Contains(x.CityCode)).ToList(); areaList.Clear(); if (areas!=null) { areas.ForEach((t) => { areaList.Add(t); } ); } cityList.Clear(); if (citys != null) { citys.ForEach((t) => { cityList.Add(t); } ); } } } catch (Exception ex) { throw ex; } } /// <summary> /// 命令是否可以執(zhí)行 /// </summary> /// <returns></returns> bool CanSelectionChangedExecute() { return true; } /// <summary> /// 創(chuàng)建新命令 /// </summary> public ICommand ProviceChangedAction { get { return new Command.ProvinceChangedCommand<object>(ProviceSelectionChangedExecute, CanSelectionChangedExecute); } } }

12.在Visual Studio 2022中按F5鍵,啟動(dòng)WPF應(yīng)用程序。然后使用鼠標(biāo)點(diǎn)擊省份下拉框,能夠看到,界面中DataGrid中的數(shù)據(jù),隨著下拉框的變化而隨之變化。如下圖。

WPF入門教程系列二十八 ——DataGrid使用示例MVVM模式(6)

?

到了這里,關(guān)于WPF入門教程系列二十八 ——DataGrid使用示例MVVM模式(6)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • WPF入門教程系列二十九 ——DataGrid使用示例MVVM模式(7)

    WPF入門教程系列二十九 ——DataGrid使用示例MVVM模式(7)

    WPF入門教程系列目錄 WPF入門教程系列二——Application介紹 WPF入門教程系列三——Application介紹(續(xù)) WPF入門教程系列四——Dispatcher介紹 WPF入門教程系列五——Window 介紹 WPF入門教程系列十一——依賴屬性(一) WPF入門教程系列十五——WPF中的數(shù)據(jù)綁定(一) ? 接上文 WPF入門教程

    2024年02月10日
    瀏覽(27)
  • WPF入門教程系列二十七 ——DataGrid使用示例MVVM模式(4)

    WPF入門教程系列目錄 WPF入門教程系列二——Application介紹 WPF入門教程系列三——Application介紹(續(xù)) WPF入門教程系列四——Dispatcher介紹 WPF入門教程系列五——Window 介紹 WPF入門教程系列十一——依賴屬性(一) WPF入門教程系列十五——WPF中的數(shù)據(jù)綁定(一) ? ? ? 計(jì)算機(jī)界的頂

    2024年02月07日
    瀏覽(24)
  • WPF入門教程系列三十 ——DataGrid驗(yàn)證

    WPF入門教程系列三十 ——DataGrid驗(yàn)證

    WPF入門教程系列目錄 WPF入門教程系列二——Application介紹 WPF入門教程系列三——Application介紹(續(xù)) WPF入門教程系列四——Dispatcher介紹 WPF入門教程系列五——Window 介紹 WPF入門教程系列十一——依賴屬性(一) WPF入門教程系列十五——WPF中的數(shù)據(jù)綁定(一) ? ???????? DataG

    2024年02月12日
    瀏覽(27)
  • WPF入門教程系列一——基礎(chǔ)

    WPF入門教程系列一——基礎(chǔ)

    一、?前言?? ?? ????? 最近在學(xué)習(xí)WPF,學(xué)習(xí)WPF首先上的是微軟的MSDN,然后再搜索了一下網(wǎng)絡(luò)有關(guān)WPF的學(xué)習(xí)資料。為了溫故而知新把學(xué)習(xí)過程記錄下來,以備后查。這篇主要講WPF的開發(fā)基礎(chǔ),介紹了如何使用Visual?Studio?2013創(chuàng)建一個(gè)WPF應(yīng)用程序。 首先說一下學(xué)習(xí)WPF的基礎(chǔ)知

    2024年02月07日
    瀏覽(32)
  • MaterialDesignInXAML WPF入門教程 快速入門

    MaterialDesignInXAML WPF入門教程 快速入門

    先去MaterialDesignInXAML下載下來源碼,以及Releases,在DemoApp 中就可以看到實(shí)際的效果很驚艷了。 除了要有一定的C#、winform 基礎(chǔ)外,建議先學(xué)習(xí)一下 XAML,對(duì)整個(gè)開發(fā)環(huán)境有個(gè)基礎(chǔ)的了解,再來學(xué)習(xí)此教程。 可以去bilibili上免費(fèi)學(xué)習(xí)一下。教程一共12個(gè)小時(shí),如果不看后面的實(shí)戰(zhàn)

    2024年02月05日
    瀏覽(28)
  • WPF真入門教程01--WPF簡(jiǎn)介

    ? ? ? ? Windows Presentation Foundation (簡(jiǎn)稱 WPF),WPF是微軟推出的基于Windows 的用戶界面框架,屬于.NET Framework 3.0的一部分。它提供了統(tǒng)一的編程模型、語(yǔ)言和框架,真正做到了分離界面設(shè)計(jì)人員與開發(fā)人員的工作;同時(shí)它提供了全新的多媒體交互用戶圖形界面。因與“我佩服”拼

    2024年02月06日
    瀏覽(24)
  • WPF真入門教程02--新建WPF工程

    WPF真入門教程02--新建WPF工程

    在VS開發(fā)環(huán)境安裝完成之后,首先我們先新建一個(gè)WPF工程,然后對(duì)工程目錄結(jié)構(gòu)啥的要有所了解才行。 打開VS2019 ? ? ?工程建好之后,WPF應(yīng)用程序”會(huì)在“引用”里面自動(dòng)添加下圖中所示的?PresentationCore、PresentationFramework、WindowsBase三大核心程序集,就是下面這個(gè)樣子 ? 默認(rèn)

    2024年02月03日
    瀏覽(26)
  • WPF 入門教程DockPanel介紹

    WPF 入門教程DockPanel介紹

    在 DockPanel中 可以很容易地??吭谒兴膫€(gè)方向的內(nèi)容(上,下,左,右)。這使它在許多情況下成為一個(gè)很好的選擇,您希望將窗口劃分為特定區(qū)域,特別是因?yàn)槟J(rèn)情況下,DockPanel 內(nèi)的最后一個(gè)元素,除非此功能被明確禁用,否則將自動(dòng)填充其余空間(中心)。 我們?cè)?/p>

    2024年02月05日
    瀏覽(37)
  • WPF教程_編程入門自學(xué)教程_菜鳥教程-免費(fèi)教程分享

    WPF教程 WPF - 概述 WPF - 環(huán)境設(shè)置 WPF - Hello World WPF - XAML概述 WPF - Elements Tree WPF - 依賴屬性 WPF - 路由事件 WPF - 控件 WPF - 布局 WPF - 布局嵌套 WPF - 輸入 WPF - 命令行 WPF - 數(shù)據(jù)綁定 WPF - 資源 WPF - 模板 WPF - 樣式 WPF - 觸發(fā)器 WPF - 調(diào)試 WPF - 自定義控件 WPF - 異常處理 WPF - 本地化 WPF - 互

    2023年04月27日
    瀏覽(27)
  • WPF 入門教程Grid使用技巧

    WPF 入門教程Grid使用技巧

    在上一章中,我們向您介紹了出色的 Grid 面板,并向您展示了一些有關(guān)如何使用它的基本示例。在本章中,我們將進(jìn)行一些更高級(jí)的布局,因?yàn)檫@是 Grid 真正閃耀的地方。首先,讓我們加入更多的列甚至一些行,以獲得真正的表格布局: 總共九個(gè)按鈕,每個(gè)按鈕都放置在自己

    2024年02月06日
    瀏覽(31)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包