我是WPF的新手,遇到绑定问题,一直在寻找不同的解决方案,但没有任何帮助。 有2个Observable Collections,我希望被绑定到listview的不同列。 XAML代码:
<GroupBox Header="Search results" HorizontalAlignment="Left" Margin="10,0,0,0" VerticalAlignment="Top" Height="727" Width="739" FontFamily="Malgun Gothic" FontSize="24">
<ListView x:Name="ListView1" HorizontalAlignment="Left" Width="729" FontFamily="Malgun Gothic" FontSize="24" Background="{x:Null}" Margin="0,0,-2,0" ItemsSource="{Binding ElementName=Search, Path=Library}">
<ListView.View>
<GridView>
<GridViewColumn Header="Song name" DisplayMemberBinding="{Binding Track}" Width="550"/>
<GridViewColumn Header="Artist" DisplayMemberBinding="{Binding Artist}" Width="170"/>
</GridView>
</ListView.View>
<ListView.DataContext>
<Classes:Library/>
</ListView.DataContext>
<Classes:Library Text=""/>
</ListView>
</GroupBox>
来自xaml.cs
Library CallSearch = new Library();
Playlist Songs = new Playlist();
private void Button_Click_2(object sender, RoutedEventArgs e)
{
CallSearch.Text = textSearch.Text;
CallSearch.Search();
}
来自班级:
private ObservableCollection<string> track = new ObservableCollection<string>();
private ObservableCollection<string> artist = new ObservableCollection<string>();
public ObservableCollection<string> Track
{
get { return track; }
set { track = value; }
}
public ObservableCollection<string> Artist
{
get { return artist; }
set { artist = value; }
}
在公共空间内:
foreach (DirectoryInfo diNext in dirs)
{
foreach (FileInfo test in diNext.GetFileSystemInfos("*"+tb+"*"+".mp3", SearchOption.AllDirectories))
{
u.Read(test.FullName);
Track.Add(u.Title);
Artist.Add(u.Artist);
}
}
我缺少什么?
更新NIT: System.Windows.Data错误:40:BindingExpression路径错误:'对象'''库'(HashCode = 36404074)'上找不到'相册'属性。 BindingExpression:路径=相册; DataItem ='Library'(HashCode = 36404074); target元素是'ListView'(Name ='ListView1'); target属性是'ItemsSource'(输入'IEnumerable') System.Windows.Data错误:40:BindingExpression路径错误:'对象'''库'(HashCode = 40706798)'上找不到'Track'属性。 BindingExpression:路径=跑道; DataItem ='Library'(HashCode = 40706798); target元素是'TextBlock'(Name =''); target属性是'Text'(类型'String') System.Windows.Data错误:40:BindingExpression路径错误:'object'''Library'(HashCode = 40706798)上找不到'Artist'属性。 BindingExpression:路径=艺术家; DataItem ='Library'(HashCode = 40706798); target元素是'TextBlock'(Name =''); target属性是'Text'(类型'String')
答案 0 :(得分:1)
不要使用两个脱节的集合,而是创建一个包含两个属性的模型类
public class Album
{
public string Track{get;set;}
public string Artist{get;set;}
}
并创建此类的ObservableCollection
public ObservableCollection<Album> Albums{get;set;}
foreach (DirectoryInfo diNext in dirs)
{
foreach (FileInfo test in diNext.GetFileSystemInfos("*"+tb+"*"+".mp3", SearchOption.AllDirectories))
{
u.Read(test.FullName);
Album album = new Album;
album.Track = u.Title;
album.Artist = u.Artist;
Albums.Add(album);
}
}
现在将此Collection绑定到ListView
<ListView x:Name="ListView1" HorizontalAlignment="Left" Width="729" FontFamily="Malgun Gothic" FontSize="24" Background="{x:Null}" Margin="0,0,-2,0" ItemsSource="{Binding Path=Albums}">
<ListView.View>
<GridView>
<GridViewColumn Header="Song name" DisplayMemberBinding="{Binding Track}" Width="550"/>
<GridViewColumn Header="Artist" DisplayMemberBinding="{Binding Artist}" Width="170"/>
</GridView>
</ListView.View>
<ListView.DataContext>
<Classes:Library/>
</ListView.DataContext>
<Classes:Library Text=""/>
</ListView>