![](/img/trans.png)
[英]Delete and reorder ListViewItems of a ListView in Windows 10 Universal Apps?
[英]Windows Universal Apps - Displaying SQLite data in Listview
我一直在嘗試這樣做,並且沒有太多代碼。.我將向您展示我的Meds.xaml和Meds.xaml.cs代碼,看看是否可以得到一些幫助將數據綁定到列表視圖。 基本上,將填寫一個表單,單擊一個按鈕,將其添加到數據庫並更新ListView。
葯物
<TextBlock x:Name="textBlock1" HorizontalAlignment="Left" TextWrapping="Wrap" Text="Medication Name:" VerticalAlignment="Top" Margin="43,255,0,0" Foreground="#FFC6C6C6" FontSize="18.667"/>
<TextBox x:Name="medname_box" HorizontalAlignment="Left" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Margin="265,248,0,0" Width="186"/>
<TextBlock x:Name="textBlock1_Copy" HorizontalAlignment="Left" TextWrapping="Wrap" Text="Medication Total Dose:" VerticalAlignment="Top"
....並不斷
<ListView x:Name="listView" HorizontalAlignment="Left" Height="464" VerticalAlignment="Top" Width="283" Margin="655,217,0,0" SelectionChanged="listView_SelectionChanged"/>
我要在哪里顯示數據
</Grid>
現在,這里是Meds.xaml.cs
public sealed partial class Meds : Page
{
string path;
SQLite.Net.SQLiteConnection conn;
public Meds()
{
this.InitializeComponent();
path = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path,
"meds.sqlite");
conn = new SQLite.Net.SQLiteConnection(new
SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), path);
conn.CreateTable<Medications>();
var query = conn.Table<Medications>();
string id = "";
string MedName = "";
string MedDose = "";
int AM;
int Noon;
int Supper;
int Night;
int PRN;
int Other;
string WhatFor = "";
}
private void button1_Click(object sender, RoutedEventArgs e)
{
if (Frame.CanGoBack)
{
Frame.GoBack();
}
}
private void button2_Click(object sender, RoutedEventArgs e)
{
var z = conn.Insert(new Medications()
{
MedName = medname_box.Text,
MedDose = meddose_box.Text,
還沒有弄清楚復選框...
// AM = ,
// Noon = "",
// Supper = "",
// Night = "",
// PRN = "",
// Other = "",
WhatFor = whatfor_box.Text
});
// var query = conn.Table<Medications>();
// string id = "";
// string CurrentMood = "";
// foreach (var message in query)
// {
// id = id + " " + message.Id;
// CurrentMood = message.MedName;
// }
}
private void listView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
}
}
public class Medications
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string MedName { get; set; }
public string MedDose { get; set; }
public int AM { get; set; }
public int Noon { get; set; }
public int Supper { get; set; }
public int Night { get; set; }
public int PRN { get; set; }
public int Other { get; set; }
public string WhatFor { get; set; }
}
看看我是否可以獲得一些將數據綁定到Listview的幫助。 基本上,將填寫一個表單,單擊一個按鈕,將其添加到數據庫並更新ListView。
根據您的描述並根據您上面提供的代碼,我制作了一個簡單的示例,該示例顯示了如何使用Binding和SQLite.Net-PCL在ListView中顯示SQLite數據供您參考:
Meds.xaml :
<StackPanel>
<TextBox Header="Medication Name: " x:Name="medname_box" HorizontalAlignment="Left" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="186" />
<TextBox Header="Medication Total Dose: " x:Name="meddose_box" HorizontalAlignment="Left" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="186" />
<TextBox Header="What For: " x:Name="whatfor_box" HorizontalAlignment="Left" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="186" />
<Button x:Name="btnAdd" Content="Add" Click="btnAdd_Click" Width="186" Margin="0,5" />
<Button x:Name="btnBack" Content="Back" Click="btnBack_Click" Width="186" Margin="0,5" />
<ListView Header="Medications" x:Name="myList" Background="White" HorizontalAlignment="Left" Width="400">
<ListView.ItemTemplate>
<DataTemplate>
<Grid Width="400">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<!--Bind data to ListView Item, here I just bind three items for Medications, you can add other items if needed-->
<TextBlock x:Name="medName" Text="{Binding Path=MedName}" TextWrapping="Wrap" />
<TextBlock Grid.Column="1" x:Name="medDose" Text="{Binding Path=MedDose}" TextWrapping="Wrap" />
<TextBlock Grid.Column="2" x:Name="whatFor" Text="{Binding Path=WhatFor}" TextWrapping="Wrap" />
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackPanel>
Meds.xaml.cs :
public sealed partial class Meds : Page
{
private string DBPath { get; set; }
public Meds()
{
this.InitializeComponent();
// Create the Database
DBPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "meds.sqlite");
using (SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), DBPath))
{
conn.CreateTable<Medications>();
// Set ItemsSource to the sqlite data for ListView
myList.ItemsSource = conn.Table<Medications>();
}
}
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
using (SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), DBPath))
{
// Add Medications
conn.Insert(new Medications
{
MedName = medname_box.Text,
MedDose = meddose_box.Text,
WhatFor = whatfor_box.Text
});
// Update the ItemsSource for ListView
myList.ItemsSource = conn.Table<Medications>();
}
}
private void btnBack_Click(object sender, RoutedEventArgs e)
{
if (Frame.CanGoBack)
{
Frame.GoBack();
}
}
}
這是整個樣本 ,下面是輸出:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.