スタック パネルを作成し、それをリスト ボックスに追加しました。スタック パネルには 2 つのテキスト ブロックが含まれています。最初のテキスト ブロックのテキストは id で、その他のテキストは name です。データベースから取得します。特定の id のスタック パネルを削除したい。コードは次のとおりです。
public partial class MainPage : PhoneApplicationPage
{
StackPanel stackpanel2 = null;
Border br = null;
TextBox tb1 = null;
TextBox tb2 = null;
string str1 = null;
TextBlock tblk1 = null;
TextBlock tblk2 = null;
ListBox lb = null;
public MainPage()
{
InitializeComponent();
createListBox();
}
public void createListBox()
{
updateDatabase();
deleteRow(i);
lb = new ListBox();
lb.Margin = new Thickness();
lb.Height = double.NaN;
lb.Width = double.NaN;
ContentPanel.Children.Add(lb);
int rowCount = noofrows();
for (int i = 1; i <= rowCount; i++)
{
createStackPanel(i);
createTextBlock1(i);
createTextBlock2(i);
lb.Items.Add(br);
}
}
public void createTextBlock1(int y )
{
tblk1 = new TextBlock();
tblk1.Height = 80;
tblk1.Width = 150;
tblk1.FontSize = 30;
tblk1.Foreground = new SolidColorBrush(Colors.White);
// tblk1.Margin = new Thickness();
tblk1.Text = returnID(y);
stackpanel2.Children.Add(tblk1);
}
public void createTextBlock2(int a)
{
tblk2 = new TextBlock();
tblk2.Height = 80;
tblk2.Width = 150;
tblk2.FontSize = 30;
tblk2.Foreground = new SolidColorBrush(Colors.White);
//tblk2.Margin = new Thickness();
tblk2.Text = SelectName(a);
stackpanel2.Children.Add(tblk2);
}
private void deleteRow(int x)
{
deleteStackPanel(x);
string str2 = "delete from Details where id =" + x;
(Application.Current as App).db.SelectList(str2);
}
private void updateDatabase()
{
string str1 = "insert into Details (id,name,age,contact) values('1','sanjay','20','4444')";
(Application.Current as App).db.SelectList(str1);
}
void createStackPanel(int c)
{
br = new Border();
br.BorderBrush = new SolidColorBrush(Colors.Green);
br.BorderThickness = new Thickness(5);
stackpanel2 = new StackPanel();
stackpanel2.Height = 100;
stackpanel2.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
stackpanel2.Margin = new Thickness();
stackpanel2.Orientation = System.Windows.Controls.Orientation.Horizontal;
stackpanel2.Background = new SolidColorBrush(Colors.Blue);
br.Child = stackpanel2;
stackpanel2.Tap += (s, e) =>
{
RowValue.rowcount = c ;
NavigationService.Navigate(new Uri("/Details.xaml", UriKind.Relative));
};
}
public String SelectName(int x)
{
return Convert.ToString((Application.Current as App).db.SelectList("select name from details where id ="+x));
}
public string returnID(int z)
{
return Convert.ToString((Application.Current as App).db.SelectList("select id from details where id ="+z));
}
public Int32 noofrows()
{
int b = Convert.ToInt32((Application.Current as App).db.SelectList("select count(*) from details"));
return b;
}
public void deleteStackPanel(int x)
{
lb.Items.Remove(stackpanel2);
}
}
public static class RowValue
{
public static int rowcount = 0;
}