2009-07-10 12 views
0

WPFでのデータバインディングのモデルに問題が発生しました。私は、オブジェクトのコレクション(変数)を持っているオブジェクトを持っていて、それぞれはさらにオブジェクト(VariableCode)の別のコレクションを持っています。複数のコレクションを1つのコレクションとして公開する

私ができる必要があるのは、各メンバオブジェクトが条件を満たしている最下位レベルのコレクション(VariableCode)の融合である最高レベルのオブジェクトから単一のコレクションを何らかの形で公開することです。そのコレクションをListBoxにバインドするには、1つ以上の変数に属するVariableCodeが表示されるはずです。私が本当にしたいのですがどのような

public class CrossTab 
{ 
    public VariableList Variables{get; set;} 
} 
public class Variable 
{ 
    public VariableCodeList VariableCodes{get; set;} 
} 
public class VariableCode 
{ 
    public bool IsShown{get; set;} 
} 

がすべて含まれているVariableCodes上のビューがあるクロスタブ(好ましくのObservableCollection < VariableCode>)のプロパティを公開します:

だからコードは次のようになりますIsShown == trueの場合。現在、それらは別々のコレクションです。それぞれは独自のVariableオブジェクトに含まれています。

public class CrossTab 
{ 
    public ObservableCollection<VariableCode> ShownCodes 
    { 
    get 
    { 
     //This is where I could picture some brute force looping 
     // and building of a new collection - but that's not really 
     // what I'm after. What I want is a live view on the data 
     // that's in there 
    } 
    } 
} 

私が驚いていたブルートフォースコードは、正しいデータを返します。ライブビューではなく静的コレクションです。

ObservableCollection<VariableCode> output = new ...(); 
Variables.ForEach(v => 
    v.VariableCodes.Where(vc => vc.IsShown) 
    .ForEach(vc => output.Add(vc)) 
); 
return output; 

考えられますか?そんなことは可能ですか?

答えて

1

次のようにあなたは、これを達成するためにSelectMany LINQのメソッドを使用することができます。

また
public class CrossTab 
{ 
    public VariableList Variables { get; set; } 

    public ObservableCollection<VariableCode> ShownCodes 
    { 
     get 
     { 
      return new ObservableCollection<VariableCode>(
       Variables 
        .SelectMany(variable => variable.VariableCodes) 
        .Where(code => code.IsShown) 
       ); 
     } 
    } 
} 
+0

うまくコンテキストに入れます。 –

1

LINQクエリを使用して、データ構造に必要な結果を得るには、プロパティを追加することを検討しましたか?

3

私はSelectMany(LINQ)があなたが探しているものだと思います。
thisが役立つかどうかを確認してください。

上記のリンクを例の&(コンパイラなし)で書いてみてください。
これが動作するかどうか、私は、驚かれることでしょう。)

var allTeams = 
from v in Variables.SelectMany(v => v.VariableCodes).SelectMany(vc) 
where vc.IsShown == true; 
select vc; 
+0

正しいです。ありがとう。 –

0

私はインデックス演算子をオーバーライドすることは良いアイデアだと思います。次のコードは、これを行う方法の例です。このコードは完全にテストされ、 "IndexOutOfRange"例外を検出します。

class Collection 
    { 
    //         [0] [1] 
    static String[] a1 = new String[] { "one", "two" }; 
    //         [2]  [3] 
    static String[] a2 = new String[] { "three", "four" }; 
    //         [4]  [5] 
    static String[] a3 = new String[] { "five", "six" }; 

    static Object[] collections = new Object[] { a1, a2, a3 }; 

    public String this[int i] 
     { 
     get 
     { 
     int add_to_index = 0; 
     int collection_num = 0; 

     int calculated_index = i - add_to_index; 

     for (String[] pointer = (String[])collections[collection_num]; 
      1 == 1; 
      pointer = (String[])collections[++collection_num], 
      calculated_index = i - add_to_index) 
      { 
      if (calculated_index < pointer.Length) 
       return pointer[calculated_index]; 
      else if (collection_num == (collections.Length - 1)) 
       throw new IndexOutOfRangeException(); 
      add_to_index += pointer.Length; 
      } 

     throw new IndexOutOfRangeException(); 
     } 
     set { throw new NotImplementedException(); } 
     } 

    } 
関連する問題