0

私は Telerik コンボボックスを使用していますが、質問は標準の wpf コンボボックスに関連していると思います。コントロールは、このオブジェクトが次のように見える「TableRecord」の監視可能なコレクションにバインドされます。

public enum RecordState
{
    Orginal, Added, Modified, Deleted
}

public class TableRecord<T> 
{
    public Guid Id { get; set; }
    public string DisplayName { get; set; }
    public T Record { get; set; }
    public RecordState State { get; set; }

    public TableRecord(Guid id, string displayName, T record, RecordState state)
    {
        Id = id;
        DisplayName = displayName;
        Record = record;
        State = state;
    }
}

これらの「TableRecords」は、次のように保持および公開されます。

private ObservableCollection<TableRecord<T>> _recordCollection = new ObservableCollection<TableRecord<T>>();
public ObservableCollection<TableRecord<T>>  Commands 
{
    get
    {
           return _recordCollection;
    }
}

私のxamlは次のようになります:

<telerik:RadComboBox ItemsSource="{Binding Commands}" DisplayMemberPath="DisplayName" SelectedValuePath="Id" Height="22" SelectedItem="{Binding SelectedCommand, Mode=TwoWay}" />

私がやりたいことは、(可能であれば) xaml を変更して、「State」値が「Deleted」に設定されているアイテムを除いて、コレクション内のすべてのアイテムを表示することです。

過去にコンテンツに基づいてテキストの色を設定するためにデータトリガーを使用したことがあるので、データトリガーを使用してこれを行うことができるかもしれないという考えがありますが、必要な方法でフィルタリングできるかどうかはわかりません。

4

1 に答える 1

2

最善の方法は、フィルタリングに CollectionViewSource を使用することです。リソースでコレクション ビュー ソースを定義し、キーを設定します。

<Window.Resources>
    <CollectionViewSource Source="{Binding Commands}" x:Key="source"/>
</Window.Resources>
<Grid>
    <ComboBox VerticalAlignment="Center" HorizontalAlignment="Center" Width="200" 
              ItemsSource="{Binding Source={StaticResource source}}"
              DisplayMemberPath="DisplayName"/>
</Grid>

コード ビハインドで、コレクション ビュー ソースの Filter コールバックを設定し、

    private void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        var source = this.Resources["source"] as CollectionViewSource;
        source.Filter += source_Filter;
    }

    private void source_Filter(object sender, FilterEventArgs e)
    {
        if (((TableRecord) e.Item).State == RecordState.Deleted)
        {
            e.Accepted = false;
        }
        else
        {
            e.Accepted = true;
        }
    }
于 2013-06-05T11:09:19.250 に答える