コンポーネントのラインナップListboxな再描画
質問
いlistboxに定義され話サイトの最新トレンドとして:
<ListBox x:Name="directoryList"
MinHeight="100"
Grid.Row="0"
ItemsSource="{Binding Path=SelectedDirectories}"/>
のSelectedDirectoriesは財産のリストDataContextのタイプ List<DirectoryInfo>
のクラスであるdatacontextのlistboxを実装しINotifyPropertyChanged.時の変更、項目の追加に成功したのリストが表示は更新されないまでのlistboxに再描画によるサイズ変更します。
そのアイデアですが?
編集:INotifyPropertyChanged実施
public class FileScannerPresenter : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private FileScanner _FileScanner;
public FileScannerPresenter()
{
this._FileScanner = new FileScanner();
}
public List<DirectoryInfo> SelectedDirectories
{
get
{
return _FileScanner.Directories;
}
}
public void AddDirectory(string path)
{
this._FileScanner.AddDirectory(path);
OnPropertyChanged("SelectedDirectories");
}
public void OnPropertyChanged(string property)
{
if (this.PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
解決
試してみてください。
ObservableCollection<DirectoryInfo>
の代わりに - あなたは理由もなく全体のListBoxのリフレッシュをトリガしている、とあなたはあなたのホスティングクラスはINotifyPropertyChangedの実装を行う必要はありません - それだけで簡単に画面のプロパティである可能性があります。キーは、新しいインスタンスにプロパティを設定しないことです。だから、ます:
class SomeWindow : Window {
public ObservableCollection<DirectoryInfo> SelectedDirectories {get; private set;}
SomeWindow() { SelectedDirectories = new ObservableCollection<DirectoryInfo>(); }
public void AddDirectory(string path) {
SelectedDirectories.Add(new DirectoryInfo(path));
}
}
あなたがそのFileScannerクラスを使用して終了した場合は、代わりにINotifyCollectionChangedを実装する必要がある - 。そのように、リストボックスを動的に追加/削除するかを知っている。
他のヒント
(更新).コンポーネントのラインナップのようである。たいコードに、新しいプロジェクト の開発に数カ月の更新またボタンをクリックしを呼び出すときAddDirectory.すいませんよりコードに変わります。 問題のような..ある複数スレッドにUI?
私はビーチに位置してFileScannerタイプです。で作成したダミーとしております。
public class FileScanner
{
string _path;
public FileScanner()
{ _path = @"c:\"; }
public List<DirectoryInfo> Directories
{
get
{
return Directory.GetDirectories(_path).Select(path => new DirectoryInfo(path)).ToList();
}
}
internal void AddDirectory(string path)
{ _path = path; }
}
の変更は出来ませんのでごFileScannerPresenterクラスです。ごlistbox ー.作成したウィンドウとDockPanelを含むおlistboxには、テキストボックス、ボタンを押します。
更新: ポールBettsについてはその通りである。この作品が戻って新規リストの各時から行きます。データバインディングリストは常にヘヴンリースキーリゾートます。りぁ、簡単なことです:
- 作FileScanner#ディレクトリを返します
ObservableCollection<DirectoryInfo>
を実装するINotifyCollectionChanged
ます。変更はすべての署名に戻りこの型の代わりにList<DirectoryInfo>
FileScannerとFileScannerPresenterないを実施するINotifyXXXインターフェース。
// in FileScanner class def public ObservableCollection<DirectoryInfo> Directories { get { return _DirList; } } internal void AddDirectory(string path) { _path = path; //var newItems = Directory.GetDirectories(_path).Select(thePath => new DirectoryInfo(thePath)).ToList(); //_DirList.Concat( newItems ); -- doesn't work for some reason. foreach (var info in Directory.GetDirectories(_path).Select(thePath => new DirectoryInfo(thePath)).ToList()) { _DirList.Add(info); } }