0
爲什麼不能使用此綁定?WPF中的ListView綁定未顯示預期的收集項目
<Window x:Class="S3PackageInstaller.MainWindow" x:Name="Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s3u="clr-namespace:S3PackageInstaller"
Icon="App.ico" Title="Sims 3 Package Installer" Height="480" Width="740">
<DockPanel LastChildFill="True">
<DockPanel DockPanel.Dock="Left" Width="200" VerticalAlignment="Stretch" LastChildFill="True"
Margin="20,20,0,20">
<!-- this is the binding that isn't working -->
<ListView Width="200" ItemsSource="{Binding ElementName=Window1, Path=InstalledPackages}">
<ListView.View>
<GridView>
<GridViewColumn Header="Installed Packages" DisplayMemberBinding="{Binding Filename}"></GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<!-- snip -->
</Window>
相關的代碼背後:
public partial class MainWindow : Window
{
public ObservableCollection<object> InstalledPackages { get; private set; }
public MainWindow()
{
InitializeComponent();
InstalledPackages = new ObservableCollection<object>();
LoadInstalledPackages();
}
private void LoadInstalledPackages()
{
var installPath = Settings.Default.TargetDirectory;
var packages = System.IO.Directory.GetFiles(installPath, "*.package");
InstalledPackages.Clear();
foreach (string filename in packages)
InstalledPackages.Add(new { Filename = filename });
}
// snip...
}
當我運行該程序,ListView控件是空的。調試時,我已驗證集合包含LoadInstalledPackages運行後的項目。
嗯..我發誓我早些時候嘗試過,但現在這樣做修復它,所以,歡呼! – Chris 2012-07-23 05:53:58