[英]create buttons into listbox using ObservableCollection in wp8 C#
在我的Windows Phone应用程序中,我想动态添加按钮当我单击“ Add
按钮时,如图所示:
下面是我在“ Add
按钮上使用的代码。
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代码
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
}
}
}
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;
}
}
}
但是我在下面这行得到了错误:public ObservableCollection<Group> groupbtn;
在这个参考groupbtn
Inconsistent accessibility: field type 'System.Collections.ObjectModel.ObservableCollection<GetContacts.Group>' is less accessible than field
请建议我,等待您的回复。 谢谢。
您每次都在创建新列表,因此在给定的时间,它只会有一个您最近添加的按钮。
因此,与其在本地定义List<Button> buttons = new List<Button>()
不如定义它。 所以要么使这个集合成员变量
或使用适当的MVVM正确执行此操作。 (这仍然不是使用MVVM的正确示例,但尝试使其与您的解决方案尽可能接近)我建议您定义如下属性(实现INotifyPropertyChanged)
public ObservableCollection<string> Buttons{get;set;}
public string ButtonName{get;set;}
并绑定您的文本框,并为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>
现在只需在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;
}
}
不一致的可访问性错误:通常是由于您的字段或类是私有的而发生的。 您必须将其更改为公开
像public class Group {}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.