[英]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.