如何从列表框中选择特定的堆栈面板?

时间:2012-09-12 06:12:57

标签: c# windows-phone-7

我创建了一个堆栈面板并将其添加到列表框中。 堆栈面板包含两个文本块,第一个文本块文本是id,其他文本是名称。 来自数据库。我想删除特定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;
    }

1 个答案:

答案 0 :(得分:1)

让我简化您的代码。

转到xaml并写下以下内容:

   <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <ListBox ItemsSource="{Binding Items}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Border BorderThickness="5" Background="Green">
                    <StackPanel Height="100" HorizontalAlignment="Left"
                                Orientation="Horizontal"
                                Background="Blue"   >
                            <TextBlock Height="80" Width="150" 
                                       FontSize="30" Foreground="White" 
                                       Text="{Binding Id}"/>
                            <TextBlock Height="80" Width="150" 
                                       FontSize="30" Foreground="White" 
                                       Text="{Binding Name}"/>
                        </StackPanel>
                    </Border>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>

我们声明了您在代码中所做的相同布局。

转到MainPage.xaml.cs并写下以下内容:

public partial class MainPage : PhoneApplicationPage
{
public class DataBaseEntry 
{
    public string Id {get;set;}
    public string Name {get;set;}
}

public ObservableCollection<DataBaseEntry> Items {get;set;}

public MainPage()
{
    InitializeComponent();
    Items = new ObservableCollection<DataBaseEntry>();
    PopulateListBox();

}

public void PopulateListBox()
{
    updateDatabase();
    deleteRow(i);

    int rowCount = noofrows();
    for (int i = 1; i <= rowCount; i++)
    {
        var entry = new DataBaseEntry
        {
            Id = returnID(i),
            Name = SelectName(i)
        }
        Items.Add(entry);
    }

}

//this will remove item from Items collection and updates listbox
public void DeleteItemById(int id) 
{
    var item = Items.FirstOrDefault(item => item.id == id.ToString());
    if (item != null) 
    {
        Items.Remove(item);
    }
    string str2 = "delete  from Details where id =" + id;
    (Application.Current as App).db.SelectList(str2);
}

我建议您阅读有关Xaml的更多信息 - 这是一种创建和管理用户界面的优雅方式。

如果您需要有关如何处理列表框中所选项目的更多信息,请阅读此blog post.