当前位置:网站首页>wpf dataGrid 实现单行某个数据变化 ui 界面随之响应
wpf dataGrid 实现单行某个数据变化 ui 界面随之响应
2022-07-07 12:10:00 【孔雀东南飞-深圳】
背景介绍:
一行数据 比如 有 姓名 年龄 id 号 当 id 变化 更新 id 当 年龄变化 更新年龄 以 id 作为编号 如果 id 变化了则新增 否则更改
1 封装 NotifyObject 类
public class NotifyObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
//----------------------------激发事件
public void OnPropertyChanged(string val)
{
PropertyChangedEventHandler handler = PropertyChanged;
if(handler != null)
{
handler(this, new PropertyChangedEventArgs(val));
}
}
}2 封装 model 类
public class Tags_Model : NotifyObject
{
public Tags_Model()
{
Clear();
}
public void Clear()
{
id = string.empty;
name = string.Empty;
age = string.Empty;
}
int id;
public int ID
{
get { return id; }
set
{
id = value;
OnPropertyChanged("ID");
}
}
string name;
public string Name
{
get { return name; }
set
{
name = value;
OnPropertyChanged("Name");
}
}
public string age;
public string Age
{
get
{
return age;
}
set
{
age = value;
OnPropertyChanged("Age");
}
}
}3 封装 接口类 ITest
public abstract class ITest : NotifyObject
{
protected ObservableCollection<Tags_Model> _orderItemList = null;
public void Add(Tags_Model model)
{
if (model != null)
{
System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
{
_orderItemList.Add(model);
}));
}
}
public ObservableCollection<Tags_Model> OrderItemList
{
get { return _orderItemList; }
set { _orderItemList = value; OnPropertyChanged("OrderItemList"); }
}
}
4 定义子类
public class Test : ITest
{
public Test()
{
if(_orderItemList == null)
{
_orderItemList = new ObservableCollection<Tags_Model>();
}
}
}5 定义 viewModel 类
public class vm_Test : NotifyObject
{
private Test ieas = null;
public vm_Test()
{
if ( ieas == null ) { ieas = new Test(); }
}
public Test Test
{
get { return ieas; }
set { ieas = value; OnPropertyChanged("Test"); }
}
}6 xaml
<Grid>
<DataGrid
x:Name="dataGrid"
HorizontalAlignment="Left"
Height="355"
Margin="10,55,0,0"
AutoGenerateColumns="False"
IsReadOnly="True"
Width="774"
FontFamily="微软雅黑"
FontSize="20"
ItemsSource ="{Binding Test.OrderItemList}"
>
<DataGrid.Columns>
<DataGridTemplateColumn Width="80"
Header="id">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock
x:Name="txtID"
Text="{Binding ID}"
VerticalAlignment="Center"
HorizontalAlignment="Center"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="80"
Header="name">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock
x:Name="txtID"
Text="{Binding Name}"
VerticalAlignment="Center"
HorizontalAlignment="Center"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="80"
Header="age">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock
x:Name="txtID"
Text="{Binding Age}"
VerticalAlignment="Center"
HorizontalAlignment="Center"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>6 主页面
public vm_Test _view = null;
Dictionary<string, Tags_Model> dic = null;
public MainWindow()
{
InitializeComponent();
if ( _view == null ) { _view = new vm_Test(); }
if(dic == null)
{
dic = new Dictionary<string, Tags_Model>();
}
if ( _view != null )
{
this.DataContext = _view;
}
if ( _view != null && _view.Test != null )
{
Tags_Model t = new Tags_Model()
{
ID = 1,
};
_view.Test.Add(t);
t.age = "dkladjla";
dic.Add(t.ID.ToString(), t);
t.name = "dkaldkaldd";
}
}
边栏推荐
- Did login metamask
- When FC connects to the database, do you have to use a custom domain name to access it outside?
- The delivery efficiency is increased by 52 times, and the operation efficiency is increased by 10 times. See the compilation of practical cases of financial cloud native technology (with download)
- 高等数学---第八章多元函数微分学1
- 2022-7-6 Leetcode 977.有序数组的平方
- TPG x AIDU | AI leading talent recruitment plan in progress!
- Transferring files between VMware and host
- AutoCAD - how to input angle dimensions and CAD diameter symbols greater than 180 degrees?
- Seven propagation behaviors of transactions
- 华为镜像地址
猜你喜欢

数据库系统概论-第一章绪论【概念模型、层次模型和三级模式(外模式、模式、内模式)】

《厌女:日本的女性嫌恶》摘录

2022-7-7 Leetcode 34. Find the first and last positions of elements in a sorted array

Transferring files between VMware and host

2022-7-6 sigurg is used to receive external data. I don't know why it can't be printed out

2022-7-6 Leetcode27.移除元素——太久没有做题了,为双指针如此狼狈的一天

Enregistrement de la navigation et de la mise en service du robot ROS intérieur (expérience de sélection du rayon de dilatation)

Use day JS let time (displayed as minutes, hours, days, months, and so on)

Take you to master the three-tier architecture (recommended Collection)

Flink | multi stream conversion
随机推荐
Is the spare money in your hand better to fry stocks or buy financial products?
Excuse me, I have three partitions in Kafka, and the flinksql task has written the join operation. How can I give the join operation alone
When FC connects to the database, do you have to use a custom domain name to access it outside?
Supply chain supply and demand estimation - [time series]
Excusez - moi, l'exécution a été réussie lors de l'utilisation des données de puits SQL Flink à Kafka, mais il n'y a pas de nombre dans Kafka
Cesium knows the longitude and latitude of one point and the distance to find the longitude and latitude of another point
Use day JS let time (displayed as minutes, hours, days, months, and so on)
【日常训练--腾讯精选50】231. 2 的幂
搜索框效果的实现【每日一题】
postgresql array类型,每一项拼接
[daily training] 648 Word replacement
MySQL "invalid use of null value" solution
The difference between memory overflow and memory leak
flask session伪造之hctf admin
js 获取当前时间 年月日,uniapp定位 小程序打开地图选择地点
566. Reshaping the matrix
Vmware 与主机之间传输文件
Move base parameter analysis and experience summary
Wired network IP address of VMware shared host
【AI实战】应用xgboost.XGBRegressor搭建空气质量预测模型(二)