C# WPF列表内部列表绑定
我使用嵌套列表框进行数据绑定, 这是我的列表框C# WPF列表内部列表绑定,c#,wpf,xaml,C#,Wpf,Xaml,我使用嵌套列表框进行数据绑定, 这是我的列表框 <ListBox x:Name="lst1" ItemsSource="{Binding ListDS}" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Disabled" > <ListBox.ItemTemplate> <DataTemplate>
<ListBox x:Name="lst1" ItemsSource="{Binding ListDS}" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Disabled" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" Margin="10">
<TextBlock Text="{Binding Name}"/>
<ListBox Height="300" Width="200" ItemsSource="{Binding userFiles }" Margin="0,10" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,10,0,0">
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Content="Print" Width="75" HorizontalAlignment="Right" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Margin="0,0,0,0" Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
这些是我的实体
public class UsersInfo
{
public long Id { get; set; }
public string Name { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateUploaded { get; set; }
public List<UsersFiles> userFiles { get; set; }
}
public class UsersFiles
{
public long Id { get; set; }
public string Name { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateDownloaded { get; set; }
}
公共类用户信息
{
公共长Id{get;set;}
公共字符串名称{get;set;}
public DateTime DateCreated{get;set;}
公共日期时间日期上载{get;set;}
公共列表用户文件{get;set;}
}
公共类用户文件
{
公共长Id{get;set;}
公共字符串名称{get;set;}
public DateTime DateCreated{get;set;}
公共日期时间日期下载{get;set;}
}
在我的ViewModel中,我有一个
public List<UsersInfo> ListDS{ get; set; }
public List ListDS{get;set;}
我正在ViewModel的构造函数中初始化它,这就是我填充数据的方式
UsersInfo entity = new UsersInfo();
entity.MediaFiles = new List<UsersFiles>();
UsersFiles mFiles = new UsersFiles();
mFiles.Name = "abc";
mFiles.Id = 1
entity.Name = "User name";
entity.MediaFiles.Add(mFiles);
ListDS.Add(entity);
UsersInfo entity=new UsersInfo();
entity.MediaFiles=新列表();
UsersFiles mFiles=新的UsersFiles();
mFiles.Name=“abc”;
mFiles.Id=1
entity.Name=“用户名”;
entity.MediaFiles.Add(mFiles);
添加(实体);
问题是,列表框显示为空白,没有显示任何内容,甚至打印按钮。
当我在其中添加ListBoxItem时,它显示得非常完美。
我在数据绑定方面哪里做错了?您必须实现
INotifyPropertyChanged
接口。当属性更改时,接口将通知视图
但是,如果集合中有任何新项目,您希望通知您的视图,因此最好使用能够为您做到这一点的集合。使用ObservableCollection
公开您的ListDS
public ObservableCollection<UserInfo> ListDS
{
get { return listDS; }
set
{
listDS = value;
// Call OnPropertyChanged whenever the property is updated
OnPropertyChanged("ListDS");
}
}
// Create the OnPropertyChanged method to raise the event
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
公共可观察收集列表
{
获取{return listDS;}
设置
{
listDS=值;
//每当更新属性时调用OnPropertyChanged
OnPropertyChanged(“ListDS”);
}
}
//创建OnPropertyChanged方法以引发事件
受保护的void OnPropertyChanged(字符串名称)
{
PropertyChangedEventHandler处理程序=PropertyChanged;
if(处理程序!=null)
{
处理程序(此,新PropertyChangedEventArgs(名称));
}
}
假设您的视图模型代码正确,您只需更改以下内容:
为此:
它会起作用的。请注意不正确的绑定值 你的viewmodel是什么?在哪里设置DataContext?在此处设置DataContext的确切顺序很重要,我想您在向
列表添加实体之前设置了DataContext,如果使用INotifyPropertyChanged通知您的列表的更改,则可以。谢谢,我正在使用列表。我可以使用ObservationCollection而不是列表吗?是,这两个集合都实现了IList
接口,因此您的代码不应该出现任何不兼容。ObservableCollection
的优点是它还实现了INotifyCollectionChanged
。在这个答案中,您将发现更多关于这个主题的内容:我用这个代码制作了一个示例。我不明白你到底有什么问题。单个项目在我的示例中正确显示。在显示UI后,如果代码发生更改,则不希望更新UI。