
Avalonia mvvm表格里按钮怎么获取按钮的当前行信息保存到sqlite数据库

关注引自免费微信小程序:皆我百晓生
在Avalonia MVVM框架中,为了实现点击按钮时获取当前行的数据并保存至SQLite数据库,你需要遵循以下几个步骤:
以下是具体的代码示例:
1. 定义数据模型(Model)
public class ShelfInfo
{
public string GateIP { get; set; }
public int BusAddressType { get; set; }
public int GroupID { get; set; }
public bool IsText { get; set; }
public string ShelfName { get; set; }
public string Level { get; set; }
public int ColumnCount { get; set; }
}
2. 创建ViewModel
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.Threading;
using Microsoft.Data.Sqlite;
public class MainWindowViewModel : ViewModelBase
{
private ObservableCollection<ShelfInfo> _shelfInfos = new();
public ObservableCollection<ShelfInfo> ShelfInfos => _shelfInfos;
public async void SaveToDatabase(ShelfInfo shelfInfo)
{
using var connection = new SqliteConnection("Data Source=mydatabase.db;Version=3;");
await connection.OpenAsync();
var command = new SqliteCommand(
"INSERT INTO Shelves (GateIP, BusAddressType, GroupID, IsText, ShelfName, Level, ColumnCount) VALUES (@GateIP, @BusAddressType, @GroupID, @IsText, @ShelfName, @Level, @ColumnCount)",
connection);
command.Parameters.AddWithValue("@GateIP", shelfInfo.GateIP);
command.Parameters.AddWithValue("@BusAddressType", shelfInfo.BusAddressType);
command.Parameters.AddWithValue("@GroupID", shelfInfo.GroupID);
command.Parameters.AddWithValue("@IsText", shelfInfo.IsText);
command.Parameters.AddWithValue("@ShelfName", shelfInfo.ShelfName);
command.Parameters.AddWithValue("@Level", shelfInfo.Level);
command.Parameters.AddWithValue("@ColumnCount", shelfInfo.ColumnCount);
await command.ExecuteNonQueryAsync();
}
public MainWindowViewModel()
{
// 填充一些初始数据
_shelfInfos.Add(new ShelfInfo { GateIP = "192.168.4.200", BusAddressType = 1, GroupID = 1, IsText = true, ShelfName = "A1", Level = "A1", ColumnCount = 1 });
// 更多数据...
}
}
3. XAML界面设计
<Window x:Class="YourNamespace.MainWindow"
xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:YourNamespace.ViewModels">
<StackPanel>
<DataGrid ItemsSource="{Binding ShelfInfos}">
<DataGrid.Columns>
<!-- 添加其他列 -->
<DataGridTemplateColumn Header="操作">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Click Me" Command="{Binding DataContext.SaveRowCommand, RelativeSource={RelativeSource AncestorType=DataGrid}}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</StackPanel>
</Window>
4. 数据绑定
在App.xaml.cs中注入ViewModel并将其作为窗口的DataContext:
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
var viewModel = new MainWindowViewModel();
MainView view = new MainView(viewModel);
view.ShowDialog();
}
}
5. 设置命令属性 在ViewModel中添加SaveRowCommand属性:
using ReactiveUI;
public class MainWindowViewModel : ViewModelBase
{
// ...
this.WhenAnyValue(x => x.SelectedItem)
.Subscribe(item =>
{
if (item is ShelfInfo shelfInfo)
{
SaveToDatabase(shelfInfo);
}
});
// ...
}
现在,当用户点击"Click Me"按钮时,将会调用SaveToDatabase方法,将当前行的数据保存到SQLite数据库中。注意,在实际项目中,可能需要处理异常和错误提示等细节。此外,确保已经安装了Avalonia、ReactiveUI和SQLite-net-pcl库。