-2

我是 WPF 的新手,并且遇到了绑定问题,一直在寻找不同的解决方案,但没有任何帮助。有 2 个可观察的集合,我想绑定到列表视图的不同列。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='库' (HashCode=36404074); 目标元素是'ListView'(名称='ListView1');目标属性是“ItemsSource”(类型“IEnumerable”) System.Windows.Data 错误:40:BindingExpression 路径错误:在“对象”“库”(HashCode=40706798)上找不到“跟踪”属性。绑定表达式:路径=轨道;DataItem='库' (HashCode=40706798); 目标元素是'TextBlock'(名称='');目标属性是“文本”(类型“字符串”) System.Windows.Data 错误:40:BindingExpression 路径错误:“艺术家” 在“对象”“库”(HashCode=40706798)上找不到属性。BindingExpression:路径=艺术家;DataItem='库' (HashCode=40706798); 目标元素是'TextBlock'(名称='');目标属性是“文本”(类型“字符串”)

4

1 回答 1

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);
       }
 }

现在将此集合绑定到 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>
于 2013-10-16T16:21:55.433 回答