我在使listview绑定完全起作用时遇到了问题(使用XAML和WPF的非常菜鸟)。我在这个论坛上搜索了很多东西,应该可以使用的代码似乎对我不起作用。
所以我在MainWindow代码中有一个名为FileList的局部变量,这是一个名为ReleaseFile的类的ObservableCollection:
ObservableCollection<ReleaseFile> FileList = new ObservableCollection<ReleaseFile>();
ReleaseFile类的声明如下:
public class ReleaseFile
{
public string Hash;
public long FileSize;
public string Path;
public Algorithm Algorithmtype;
public enum Algorithm { MD5, SHA1, SHA256};
public ReleaseFile(string hash, long fileSize, string path, Algorithm algorithm)
{
Hash = hash;
FileSize = fileSize;
Path = path;
Algorithmtype = algorithm;
}
}
在MainWindow中,我已将DataContext设置为FileList:
public MainWindow()
{
InitializeComponent();
DataContext = FileList;
}
这是列表视图的XAML代码:
<ListView x:Name="ReleaseFileList" HorizontalAlignment="Left" Height="399" Margin="273,10,0,0" VerticalAlignment="Top" Width="509" ItemsSource="{Binding}">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Path=Hash}" Header="Hash"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=Path}" Header="Path"/>
</GridView>
</ListView.View>
</ListView>