当前位置:网站首页>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";
}
}
边栏推荐
- 最长上升子序列模型 AcWing 1014. 登山
- Parameter keywords final, flags, internal, mapping keywords internal
- Environment configuration
- How does MySQL control the number of replace?
- How can the PC page call QQ for online chat?
- 交换机和路由器的异同
- 2022-7-7 Leetcode 844.比较含退格的字符串
- Advanced Mathematics - Chapter 8 differential calculus of multivariate functions 1
- Clickhouse (03) how to install and deploy Clickhouse
- ndk初学习(一)
猜你喜欢
Co create a collaborative ecosystem of software and hardware: the "Joint submission" of graphcore IPU and Baidu PaddlePaddle appeared in mlperf
[fortress machine] what is the difference between cloud fortress machine and ordinary fortress machine?
最长上升子序列模型 AcWing 1014. 登山
得物客服热线的演进之路
AI人才培育新思路,这场直播有你关心的
AutoCAD - how to input angle dimensions and CAD diameter symbols greater than 180 degrees?
数据库系统概论-第一章绪论【概念模型、层次模型和三级模式(外模式、模式、内模式)】
2022-7-6 Leetcode 977.有序数组的平方
使用day.js让时间 (显示为几分钟前 几小时前 几天前 几个月前 )
带你掌握三层架构(建议收藏)
随机推荐
Transferring files between VMware and host
2022-7-6 Leetcode 977.有序数组的平方
Take you to master the three-tier architecture (recommended Collection)
Cargo placement problem
566. 重塑矩阵
属性关键字Aliases,Calculated,Cardinality,ClientName
The meaning of variables starting with underscores in PHP
請問,在使用flink sql sink數據到kafka的時候出現執行成功,但是kafka裏面沒有數
Vmware 与主机之间传输文件
Excerpt from "misogyny: female disgust in Japan"
Flink | multi stream conversion
[AI practice] Application xgboost Xgbregressor builds air quality prediction model (II)
FC连接数据库,一定要使用自定义域名才能在外面访问吗?
Is the compass stock software reliable? Is it safe to trade stocks?
Regular expression integer positive integer some basic expressions
Huawei image address
TPG x AIDU | AI leading talent recruitment plan in progress!
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)
When FC connects to the database, do you have to use a custom domain name to access it outside?
2022-7-7 Leetcode 844.比较含退格的字符串