WPF Datagrid contains ComboBox while display textblock when display and combobox in editing mode

2020-12-26 12:27

阅读:637

标签:item   extc   ros   input   sso   color   amp   focus   nat   

  XAML:

"WpfApplication1.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:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525"> 
    "Single" IsReadOnly="False" AutoGenerateColumns="False"  x:Name="dg"
                  ItemsSource="{Binding UsersList,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
                  RowEditEnding="DataGrid_RowEditEnding" CellEditEnding="dg_CellEditEnding">
            "Nation">
                    "{Binding UserNation,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
                        "{Binding Source={x:Static local:MainWindow.NationsList}}" SelectedIndex="0" SelectedValue="{Binding UserNation,UpdateSourceTrigger=LostFocus}"/>
                        "CanEnglish" IsThreeState="False" Binding="{Binding CanEnglish,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
                "Name"  Binding="{Binding Name,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
                
                "Grade">
                    "{Binding UserGrade,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
                        "{Binding Source={x:Static local:MainWindow.GradesList}}" SelectedIndex="0" SelectedValue="{Binding UserGrade,UpdateSourceTrigger=LostFocus}"/>
                        

CS:

using Newtonsoft.Json;
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 System.IO;
using System.Collections.ObjectModel;
using System.Data;

namespace WpfApplication1
{
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window
    { 
        public static Liststring> GradesList { get; set; }
        public List UsersList { get; set; }
        public static Liststring> NationsList { get; set; }
        static MainWindow()
        {
            NationsList = new Liststring>() { "USA", "UK", "JAPAN" };
            GradesList = new Liststring>() { "1", "2", "3" };
        }

        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
            InitDataSource(); 
        }

        private void InitDataSource()
        {           
            UsersList = new List();
            string jsonValue = File.ReadAllText("users.log");
            UsersList = JsonConvert.DeserializeObject>(jsonValue);            
        }        

        public void NationSelectionChanged(object sender,SelectionChangedEventArgs e)
        {
            var comboBox = sender as ComboBox;
            if (comboBox.SelectedItem != null)
            {
                var index = dg.SelectedIndex;
                if(index>0)
                {
                    var selectedUser = dg.SelectedItem as User;
                    if(selectedUser!=null)
                    {
                        string newNation = (sender as ComboBox).SelectedItem as string;
                        selectedUser.UserNation = newNation;
                        UsersList.RemoveAt(index);
                        UsersList.Insert(index, selectedUser);
                        string json = JsonConvert.SerializeObject(UsersList, Formatting.Indented);
                        File.WriteAllText("users.log", json, Encoding.UTF8);
                    }  
                }
            }
        }

        public void GradeSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var comboBox = sender as ComboBox;
            if (comboBox.SelectedItem != null)
            {
                var index = dg.SelectedIndex;
                if (index > 0)
                {
                    var selectedUser = dg.SelectedItem as User;
                    if (selectedUser != null)
                    {
                        string newGrade = (sender as ComboBox).SelectedItem as string;
                        selectedUser.UserGrade = newGrade;
                        UsersList.RemoveAt(index);
                        UsersList.Insert(index, selectedUser);
                        string json = JsonConvert.SerializeObject(UsersList, Formatting.Indented);
                        File.WriteAllText("users.log", json, Encoding.UTF8);
                    }
                }
            }
        }

        private void DataGrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
        {            
           var selectedUser= e.Row.DataContext as User;
           if(selectedUser!=null)
            {
                UpdateUsersList(selectedUser);
            }
        }

        private void dg_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {
            var selectedUser = e.Row.Item as User;
            if(selectedUser!=null)
            {
                UpdateUsersList(selectedUser);
            }
        }

        private void UpdateUsersList(User selectedUser)
        {
            if(UsersList!=null && UsersList.Any())
            {
                var index = dg.SelectedIndex;
                if (index >= 0)
                {
                    var newUser = new User()
                    {
                        UserNation = selectedUser.UserNation,
                        Name = selectedUser.Name,
                        CanEnglish = selectedUser.CanEnglish,
                        UserId = selectedUser.UserId,
                        UserGrade = selectedUser.UserGrade
                    };
                    UsersList.RemoveAt(index);
                    UsersList.Insert(index, newUser);
                    string json = JsonConvert.SerializeObject(UsersList, Formatting.Indented);
                    File.WriteAllText("users.log", json, Encoding.UTF8);
                }
            }   
        }
    }

    public class User
    {
        public string UserNation { get; set; }
        public string Name { get; set; }
        public bool CanEnglish { get; set; }
        public int UserId { get; set; }
        public string UserGrade { get; set; }
    }

    public class Nation
    {
        public string NationName { get; set; }
        public string Code { get; set; }
    }

    public class Grade
    {
        public string GradeName { get; set; }
        public int GradeIndex { get; set; }
    }
   
}

This is the key role 

 "Grade">
                    "{Binding UserGrade,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
                        "{Binding Source={x:Static local:MainWindow.GradesList}}" SelectedIndex="0" SelectedValue="{Binding UserGrade,UpdateSourceTrigger=LostFocus}"/>
                        

 

WPF Datagrid contains ComboBox while display textblock when display and combobox in editing mode

标签:item   extc   ros   input   sso   color   amp   focus   nat   

原文地址:https://www.cnblogs.com/Fred1987/p/13375092.html

上一篇:C#程序的基本结构

下一篇:windy数


评论


亲,登录后才可以留言!