简体   繁体   English

在wp8 C#中使用ObservableCollection将按钮创建到列表框中

[英]create buttons into listbox using ObservableCollection in wp8 C#

In my windows phone application I want to add buttons dynamically When I click on Add button as you can see on the image: 在我的Windows Phone应用程序中,我想动态添加按钮当我单击“ Add按钮时,如图所示:

在此处输入图片说明

And below is the code that I am using on the Add button. 下面是我在“ Add按钮上使用的代码。

xaml page code XAML页面代码

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="24,0,0,0">
            <TextBox x:Name="tb_groupname"
                 Height="90"
                 Background="White" 
                 Margin="0,0,125,517"

                 Foreground="Blue" TextChanged="tb_groupname_TextChanged" GotFocus="tb_groupname_GotFocus" LostFocus="tb_groupname_LostFocus"/>
            <Button x:Name="btn_groupname"
                    Content="Add"
                    Background="AliceBlue"
                    Foreground="Blue"
                    FontSize="25"
                    Width="120"
                    Height="90"
                    HorizontalAlignment="Right"
                    VerticalAlignment="Top" Click="btn_groupname_Click"></Button>
            <ListBox x:Name="lb_groupofcontacts" ItemsSource="{Binding}" Margin="0,118,0,0">
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Button Content="{Binding Name}" Width="100" Height="100" Click="btn_Click"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox>
        </Grid>

xaml.cs code xaml.cs代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Phone.UserData;
using System.Windows.Media;
using Microsoft.Phone.Maps.Controls;
using System.Collections.ObjectModel;

namespace GetContacts
{
    public partial class createGroups : PhoneApplicationPage
    {
        string buttonName = "";
        public ObservableCollection<Group> groupbtn;      
        List<Button> buttons = new List<Button>();
        public createGroups()
        {
            InitializeComponent();
        }

        private void btn_groupname_Click(object sender, RoutedEventArgs e)
        {

            if (tb_groupname.Text != string.Empty)
            {
                groupbtn = new ObservableCollection<Group>()
                {
                    new Group {Name = tb_groupname.Text}
                };
                lb_groupofcontacts.DataContext = groupbtn;
            }
        }
        private void btn_Click(object sender, RoutedEventArgs e)
        {
          // some code
        }

    }
}

And Group is my class below: Group是我下面的课程:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using GetContacts.Resources;
using Microsoft.Phone.UserData;

namespace GetContacts
{
    class Group
    {
        public string Name { get; set; }

        public Group()
        { 

        }
        public Group(Button btn)
        {
            Name = btn.Name;

        }
    }
}

But I am getting error below at this line public ObservableCollection<Group> groupbtn; 但是我在下面这行得到了错误:public ObservableCollection<Group> groupbtn; and at this reference groupbtn 在这个参考groupbtn

Inconsistent accessibility: field type 'System.Collections.ObjectModel.ObservableCollection<GetContacts.Group>' is less accessible than field

Kindly suggest me, waiting for your reply. 请建议我,等待您的回复。 Thanks. 谢谢。

You are creating new list every time and hence at a given time it will just has one button that you most recently add. 您每次都在创建新列表,因此在给定的时间,它只会有一个您最近添加的按钮。

so instead of defining List<Button> buttons = new List<Button>() locally. 因此,与其在本地定义List<Button> buttons = new List<Button>()不如定义它。 So either make this collection member variable 所以要么使这个集合成员变量

OR use proper MVVM to do it correctly. 使用适当的MVVM正确执行此操作。 (Still this is not the proper example of using MVVM but trying to make it as close as possible for your solution) I would suggest you to define the properties like below (Implement INotifyPropertyChanged) (这仍然不是使用MVVM的正确示例,但尝试使其与您的解决方案尽可能接近)我建议您定义如下属性(实现INotifyPropertyChanged)

       public  ObservableCollection<string> Buttons{get;set;}
       public  string ButtonName{get;set;}

and bind your textbox and define ItemTemplate for your ListBox like: 并绑定您的文本框,并为ListBox定义ItemTemplate,如下所示:

    <TextBox x:Name="tb_groupname" Text="{Binding ButtonName}"/>
    <ListBox x:Name="lb_groupofcontacts" ItemsSource="{Binding Buttons}" Margin="0,118,0,0">
       <ListBox.ItemTemplate>
           <DataTemplate>
               <Button FontSize="25" Width="120" Height="90" Content="{Binding}"></Button>
           </DataTemplate>
       </ListBox.ItemTemplate>
    </ListBox>

now just initialize you button collection in the initialize method 现在只需在initialize方法中初始化按钮集合

     public MainWindow()
     {
        InitializeComponent();

        Buttons = ObservableCollection<string>();
        DataContext = this;
     }


private void btn_groupname_Click(object sender, RoutedEventArgs e)
{
    if (ButtonName != string.Empty)
    {
            Buttons.Add(ButtonName);
           ButtonName = string.Empty;
    }            
}

Inconsistent accessibility error: Generally this happens because your field or class is private. 不一致的可访问性错误:通常是由于您的字段或类是私有的而发生的。 You must change it to public 您必须将其更改为公开

like public class Group {} public class Group {}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM