我正在尝试创建一个联系人列表用户控件,其堆栈面板绑定ObservableCollection
到LoggedInUser
用户控制:
<UserControl.Content>
<Grid>
<Border BorderBrush="LightBlue" BorderThickness="1,1,1,1" CornerRadius="8,8,8,8" Height="350" HorizontalAlignment="Left" VerticalAlignment="Top" Width="290">
<ItemsControl x:Name="tStack" Grid.Column="0">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Height="30" Content="{Binding Username}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Border>
</Grid>
</UserControl.Content>
用户控制代码背后
public partial class ContactList : UserControl
{
public ContactList()
{
InitializeComponent();
ContactListViewModel clvm = ContactListViewModel.GetInstance();
clvm.Contacts.Add(new LoggedInUser("test", "123"));
this.DataContext = clvm.Contacts;
}
}
还有我的 ContactListViewModel
class ContactListViewModel
{
private static ContactListViewModel instance;
public ObservableCollection<LoggedInUser> Contacts = new ObservableCollection<LoggedInUser>();
public static ContactListViewModel GetInstance()
{
if (instance == null)
instance = new ContactListViewModel();
return instance;
}
}
LoggedInUser
上课,以防万一
public class LoggedInUser
{
private string username;
public string Username
{
get { return username; }
set { username = value; }
}
}
我的堆栈面板仍然是空的!帮助!