0

a を別のクラスListViewの anにバインドするのに苦労しています。ObservableCollection

私のxaml:

<ListView Height="117" HorizontalAlignment="Left" Margin="20,239,0,0" Name="lvResults" VerticalAlignment="Top" Width="759" ItemsSource="{Binding RuleSearch.FileMatches}">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding FileName}"/>
                <GridViewColumn Header="Size" Width="120" DisplayMemberBinding="{Binding DirectoryName}"/>
                <GridViewColumn Header="Date" Width="120" DisplayMemberBinding="{Binding Size}"/>
                <GridViewColumn Header="Full Path" Width="120" />
                <GridViewColumn Header="Some Meaningless Data" Width="120" />
            </GridView>
        </ListView.View>
    </ListView>

Xaml ビハインド コード:

private Search _ruleSearch = new Search();
public Search RuleSearch { get { return _ruleSearch; }}

検索クラス:

public ObservableCollection<Result> FileMatches { get; private set; }

それが違いを生む場合、変更は新しいスレッドで行われることに注意してください。

private void FindResultOnNewThreads()
  {
     FileMatches.Clear();

     Parallel.ForEach(_fileList, file =>
     {
        foreach (Regex search in SearchTermList.Where(search => search.IsMatch(file)))
        {
           lock (FileMatches)
           {
              FileInfo fileInfo = new FileInfo(file);
              FileMatches.Add(new Result
                                 {
                                    Attributes = fileInfo.Attributes,
                                    DirectoryName = fileInfo.DirectoryName,
                                    Extension = fileInfo.Extension,
                                    FileName = fileInfo.Name,
                                    FullNamePath = fileInfo.FullName,
                                    Size = fileInfo.Length
                                 });
           }
        }
     });
  }

結果クラス:

    public class Result
   {
      public string FileName { get; set; }
      public string DirectoryName { get; set; }
      public string FullNamePath { get; set; }
      public long Size { get; set; }
      public string Extension { get; set; }
      public FileAttributes Attributes { get; set; }

   }

問題は本当に、私は自分で wpf を学んでいて、WPF でデータ バインディングのルール セットを実際に見つけることができなかったことです。私は立ち往生していること以外に、プロパティとパブリックなものが必要であることを知っています。

4

2 に答える 2

1

おそらく、listViews DataContext は、RuleSearch プロップがある UserControls や Windows などに設定されていません。

xaml.cs 分離コードで設定できます。

lvResult.DataContext = this;

またはxamlで

<ListView Height="117" HorizontalAlignment="Left" Margin="20,239,0,0" Name="lvResults" VerticalAlignment="Top" Width="759" ItemsSource="{Binding Path=RuleSearch.FileMatches, RelativeSource={RelativeSource AncestorType={x:Type typeOfAncestor}}}">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding FileName}"/>
            <GridViewColumn Header="Size" Width="120" DisplayMemberBinding="{Binding DirectoryName}"/>
            <GridViewColumn Header="Date" Width="120" DisplayMemberBinding="{Binding Size}"/>
            <GridViewColumn Header="Full Path" Width="120" />
            <GridViewColumn Header="Some Meaningless Data" Width="120" />
        </GridView>
    </ListView.View>
</ListView>

typeOfAncestor は、ユーザーコントロール/ウィンドウのタイプです...

于 2012-10-12T14:28:46.960 に答える
0

Searchクラスは INotifyPropertyChanged を実装する必要があります。これは、コード ビハインドのプロパティが変更されたときに UI に通知するために使用されます (この場合は)FileMatches。これには、「FileMatches」というプロパティ名でイベントを発生さFileMatches.CollectionChangedせるイベントへのコールバックを登録することが含まれます。INotifyPropertyChanged.PropertyChanged

コレクション内の値を UI と同期させたい場合は、Result クラスに INotifyPropertyChanged も実装する必要があることに注意してください。(コードを見ると、Result クラスの内容は一定に見えるので、これを行う必要はありません)。

于 2012-10-12T14:38:15.327 に答える