当前位置:网站首页>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";
}
}
边栏推荐
- [fortress machine] what is the difference between cloud fortress machine and ordinary fortress machine?
- Vmware 与主机之间传输文件
- 2022-7-6 Leetcode 977.有序数组的平方
- 2022-7-7 Leetcode 34. Find the first and last positions of elements in a sorted array
- Beginner XML
- Redis can only cache? Too out!
- MySQL "invalid use of null value" solution
- 交换机和路由器的异同
- Did login metamask
- [network security] SQL injection syntax summary
猜你喜欢
118. Yanghui triangle
XML文件的解析操作
2022-7-6 Leetcode27. Remove the element - I haven't done the problem for a long time. It's such an embarrassing day for double pointers
js 获取当前时间 年月日,uniapp定位 小程序打开地图选择地点
2022-7-6 Leetcode 977.有序数组的平方
为租客提供帮助
带你掌握三层架构(建议收藏)
TPG x AIDU | AI leading talent recruitment plan in progress!
LeetCode简单题分享(20)
UML 状态图
随机推荐
Dry goods | summarize the linkage use of those vulnerability tools
gvim【三】【_vimrc配置】
手把手教会:XML建模
2022-7-7 Leetcode 34.在排序数组中查找元素的第一个和最后一个位置
IP address home location query
566. Reshaping the matrix
Huawei image address
wpf dataGrid 实现单行某个数据变化 ui 界面随之响应
SSRF vulnerability file pseudo protocol [netding Cup 2018] fakebook1
What are the principles for distinguishing the security objectives and implementation methods that cloud computing security expansion requires to focus on?
Social responsibility · value co creation, Zhongguancun network security and Information Industry Alliance dialogue, wechat entrepreneur Haitai Fangyuan, chairman Mr. Jiang Haizhou
Environment configuration of lavarel env
请问,我kafka 3个分区,flinksql 任务中 写了 join操作,,我怎么单独给join
请问指南针股票软件可靠吗?交易股票安全吗?
2022-7-6 Leetcode 977.有序数组的平方
Reverse non return to zero code, Manchester code and differential Manchester code of common digital signal coding
PHP中用下划线开头的变量含义
3D Detection: 3D Box和点云 快速可视化
3D detection: fast visualization of 3D box and point cloud
Build a secure and trusted computing platform based on Kunpeng's native security