WPF-DataGrid增删改查不绑定数据源
Posted wangyinlon
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF-DataGrid增删改查不绑定数据源相关的知识,希望对你有一定的参考价值。
Xaml
<DataGrid Height="200"
Name="dataGrid1"
Margin="5,0,5,0"
VerticalAlignment="Top"
AlternationCount="2"
AutoGenerateColumns="False"
CanUserAddRows="False"
CanUserDeleteRows="False"
CanUserReorderColumns="False"
CanUserResizeColumns="False"
CanUserResizeRows="False"
CanUserSortColumns="False"
IsReadOnly="True"
RowHeaderWidth="0"
SelectionMode="Single">
<DataGrid.Columns>
<DataGridTextColumn Width="8*" Header="账号" Binding="{Binding Name}"></DataGridTextColumn>
<DataGridTemplateColumn Width="2*" Header="状态" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock
Margin="0,0,0,0"
Foreground="#666666"
Text="{Binding Age}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
建立实体类
public class User : INotifyPropertyChanged
{
private string name;
public string Name
{
get
{
return name;
}
set
{
name = value;
OnPropertyChanged("Name");
}
}
public string Age { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
增删改
private void add_Click(object sender, RoutedEventArgs e)
{
int index= dataGrid1.Items.Add(new User());
}
private void delete_Click(object sender, RoutedEventArgs e)
{
if (dataGrid1.SelectedIndex >= 0)
{
dataGrid1.Items.RemoveAt(dataGrid1.SelectedIndex);
}
}
private void update_Click(object sender, RoutedEventArgs e)
{
if (dataGrid1.SelectedIndex >= 0)
{
SubRowsId().Name = "update";
}
}
#region 获取选中行的原始值
/// <summary>
/// 获取选中行的原始值
/// </summary>
/// <param name="rowindex"></param>
private User SubRowsId()
{
return (User)dataGrid1.SelectedItem;
}
#endregion
以上是关于WPF-DataGrid增删改查不绑定数据源的主要内容,如果未能解决你的问题,请参考以下文章
Elasticsearch全文检索技术 一篇文章即可从入门到精通(Elasticsearch安装,安装kibana,安装ik分词器,数据的增删改查,全文检索查询,聚合aggregations)(代码片