当前位置:网站首页>WPF DataGrid realizes the UI interface to respond to a data change in a single line
WPF DataGrid realizes the UI interface to respond to a data change in a single line
2022-07-07 14:12:00 【Peacock Flying Southeast - Shenzhen】
Background introduction :
A line of data such as Yes full name Age id Number When id change to update id When Age change Update age With id As a number If id New if changed Otherwise change
1 encapsulation NotifyObject class
public class NotifyObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
//---------------------------- Trigger event
public void OnPropertyChanged(string val)
{
PropertyChangedEventHandler handler = PropertyChanged;
if(handler != null)
{
handler(this, new PropertyChangedEventArgs(val));
}
}
}2 encapsulation model class
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 encapsulation Interface class 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 Defining subclasses
public class Test : ITest
{
public Test()
{
if(_orderItemList == null)
{
_orderItemList = new ObservableCollection<Tags_Model>();
}
}
}5 Definition viewModel class
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=" Microsoft YaHei "
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 The main page
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";
}
}
边栏推荐
- Introduction to database system - Chapter 1 introduction [conceptual model, hierarchical model and three-level mode (external mode, mode, internal mode)]
- Laravel5 call to undefined function OpenSSL cipher IV length() error php7 failed to open OpenSSL extension
- Excellent open source system recommendation of ThinkPHP framework
- [high frequency interview questions] difficulty 2.5/5, simple combination of DFS trie template level application questions
- gvim【三】【_vimrc配置】
- Reverse non return to zero code, Manchester code and differential Manchester code of common digital signal coding
- 搜索框效果的实现【每日一题】
- FCOS3D label assignment
- 【日常训练】648. 单词替换
- UML 顺序图(时序图)
猜你喜欢

VSCode 配置使用 PyLint 语法检查器

社会责任·价值共创,中关村网络安全与信息化产业联盟对话网信企业家海泰方圆董事长姜海舟先生

gvim【三】【_vimrc配置】

.net core 关于redis的pipeline以及事务

Did login metamask

libSGM的horizontal_path_aggregation程序解读

Dry goods | summarize the linkage use of those vulnerability tools

一个简单LEGv8处理器的Verilog实现【四】【单周期实现基础知识及模块设计讲解】

566. 重塑矩阵

带你掌握三层架构(建议收藏)
随机推荐
Take you to master the three-tier architecture (recommended Collection)
LeetCode简单题分享(20)
Did login metamask
Introduction to database system - Chapter 1 introduction [conceptual model, hierarchical model and three-level mode (external mode, mode, internal mode)]
The difference between memory overflow and memory leak
[fortress machine] what is the difference between cloud fortress machine and ordinary fortress machine?
Mathématiques avancées - - chapitre 8 différenciation des fonctions multivariables 1
Realization of search box effect [daily question]
SSRF vulnerability file pseudo protocol [netding Cup 2018] fakebook1
数据库系统概论-第一章绪论【概念模型、层次模型和三级模式(外模式、模式、内模式)】
Hands on Teaching: XML modeling
Supply chain supply and demand estimation - [time series]
2022-7-6 Leetcode 977. Square of ordered array
The reason why data truncated for column 'xxx' at row 1 appears in the MySQL import file
请问,redis没有消费消息,都在redis里堆着是怎么回事?用的是cerely 。
Advanced Mathematics - Chapter 8 differential calculus of multivariate functions 1
Mysql怎样控制replace替换的次数?
常用數字信號編碼之反向不歸零碼碼、曼徹斯特編碼、差分曼徹斯特編碼
AI人才培育新思路,这场直播有你关心的
118. Yanghui triangle