WPF Datagrid contains ComboBox while display textblock when display and combobox in editing mode
2020-12-26 12:27
标签:item extc ros input sso color amp focus nat XAML: CS: This is the key role 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.htmlusing 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
{
///
>(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; }
}
}
文章标题:WPF Datagrid contains ComboBox while display textblock when display and combobox in editing mode
文章链接:http://soscw.com/index.php/essay/38346.html