我使用MVVM开发我的应用程序。 [表]成员[Column] FullName |地址
我想列出要在Listbox中显示的所有FullName不知道如何...当我单击Show ...时出现“Library.Model.Member”行...(Library是我项目的名称)
我在ViewModel中的添加功能
public void Add(Member info)
{
MemberDB.Members.InsertOnSubmit(info);
MemberDB.SubmitChanges();
Data.Add(info);
}
在我的Show.xaml.cs
中 public Show()
{
InitializeComponent();
this.DataContext = App.ViewModel;
}
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
// Save changes to the database.
App.ViewModel.SaveChangesToDB();
}
列表框:
<StackPanel Orientation="Vertical">
<ListBox x:Name="FullName"
ItemsSource="{Binding Data}"
FontSize="30">
<TextBlock Text="{Binding FullName}"/>
</Listbox>
</StackPanel>
数据
private ObservableCollection<Member> _data;
public ObservableCollection<Member> Data
{
get { if(_data==null)
_data = new ObservableCollection<Member>();
return _data;
}
set
{
if(value !=_data)
_data = value;
NotifyPropertyChanged("Data");
}
答案 0 :(得分:1)
你需要给ListBox一个ItemTemplate,默认情况下它有一个ListBoxItem,它试图在你的对象上做ToString()。
像这样的东西
<ListBox ItemsSource="{StaticResource customers}" Width="350" Margin="0,5,0,10">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Padding="5,0,5,0"
Text="{Binding FirstName}" />
<TextBlock Text="{Binding LastName}" />
<TextBlock Text=", " />
<TextBlock Text="{Binding Address}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>