2010-12-07 1 views
1

オートコンプリートの値を動的に入力しようとしています。私はSearchResultタプルのリストを取得します。タプルごとに最初の文字列はKEYであり、文字列のリストは列ごとに表示する必要がある表示テキストです。すべての行には、表示テキストリストに同じ量の項目が含まれていると仮定します。F#Silverlight:Columnを通常通りバインドするPropertyPathまたは他のメソッドを使用する

[ 
    ("MSFT.OQ", ["MSFT.OQ"; "Microsoft"; "Nasdaq"]) ; 
    ("GOOG.OQ", ["GOOG.OQ"; "Google"; "Nasdaq"]); 
] 

と表示:

MSFT.OQ   Microsoft  Nasdaq 
GOOG.OQ   Google   Nasdaq 
私は本当に簡単な例は、このようなデータを取るだろう

...序数で表示テキストリストの値にバインドできるようにしたいと思います、リスト全体が各列で終わるさ

["MSFT.OQ"; "Microsoft"; "Nasdaq"]  ["MSFT.OQ"; "Microsoft"; "Nasdaq"] ["MSFT.OQ"; "Microsoft"; "Nasdaq"] 
["GOOG.OQ"; "Google"; "Nasdaq"]  ["GOOG.OQ"; "Google"; "Nasdaq"]  ["GOOG.OQ"; "Google"; "Nasdaq"] 

は、私のようなものを見ていますが、私は縛られていると思っています。

私のサンプルコードでは、(より複雑なモデルから単純化することを試み):

type SearchResult = (string * string list) 

type Template() as this = 
    inherit Page 

    [<DefaultValue>] 
    val mutable acbTickerSearch : AutoCompleteBox 

    do 
     this.acbTickerSearch = this ? acbTickerSearch 
     this.display Some(this.getSampleResults()) 

    member private this.getSampleResults() = 
     [ 
      ("MSFT.OQ", ["MSFT.OQ"; "Microsoft"; "Nasdaq"]) ; 
      ("GOOG.OQ", ["GOOG.OQ"; "Google"; "Nasdaq"]); 
      ("IBM", ["IBM"; "International Business Machines"; "NYSE"]); 
      ("AKAM.OQ", ["AKAM.OQ"; "Akamai"; "Nasdaq"]); 
     ] 

    member this.display (results: SearchResult list option) = 
     let build(result: SearchResult) = 
      // if we haven't built the bindings yet lets do so 
      if this.tickerSearchDataGrid = null then 
       // create a grid 
       this.tickerSearchDataGrid <- new DataGrid() 
       this.tickerSearchDataGrid.AutoGenerateColumns <- false 

       let addColumn i (item: string) = 
        let col = new DataGridTextColumn() 
        let binding = System.Windows.Data.Binding() 

        // LOOK HERE: attempting to bind to an indexer... not working so well,, 
        binding.Path <- PropertyPath([i]) 

        col.Binding <- binding 
        this.tickerSearchDataGrid.Columns.Add(col) 
        i + 1 

       result 
        // the second portion of the tuple, is a list that 
        // needs to be displayed, wach item in its own column 
        |> snd 
        // should probably be List.iteri 
        |> List.fold addColumn 0 
        // don't need this with List.iteri 
        |> ignore 

     let displayResults (resultLst: SearchResult list) = 
      // create a list of lists, throwing away the "key" portion of the tuple 
      // potentially a bug I need toget around... 
      let lst = 
       resultLst 
        |> List.map (fun (r: SearchResult) -> snd r) 

      // bind to the data source 
      this.tickerSearchDataGrid.ItemsSource <- lst 
      this.tickerSearchDataGrid.HeadersVisibility <- DataGridHeadersVisibility.None 
      this.acbTickerSearch.ItemsSource <- [this.tickerSearchDataGrid] 
      this.acbTickerSearch.IsDropDownOpen <- true 

     match results with 
     | None ->() 
     | Some r -> 
      // set the number of columns based on the results, 
      // assume all tuples will have an equal number of values, 
      // we only need the head to determine the columns then 
      build <| List.head r 

      // bind the results 
      displayResults r 

は、これらが同じ結果を返します(少なくとも私には)驚くほど...


をありがとう:

私にはあまり意味をしていない
binding.Path <- PropertyPath([]) 
    binding.Path <- PropertyPath("") 
    binding.Path <- PropertyPath([0].[0]) 

...

答えて

0

http://www.scottlogic.co.uk/blog/colin/2009/04/binding-a-silverlight-datagrid-to-dynamic-data-via-idictionary/

が働いた:

type RowIndexConverter() = 
    interface IValueConverter with 
     member this.Convert(value, targetType, parameter, culture) = 
      let row = value :?> string list ; 

      let index: int = parameter :?> int; 
      row.[index] :> obj; 

     member this.ConvertBack(value, targetType, parameter, culture) = raise <| NotImplementedException() 

と私のコードで

との結合を交換します
let binding = System.Windows.Data.Binding() 
binding.Path <- PropertyPath([]) 
binding.Converter <- RowIndexConverter() 
binding.ConverterParameter <- i 

I guそれは大丈夫な解決策です。うまくいけば、脳(誤植や神の介入でもありました)も同様に機能します。

0

私は実際には知らないが、私は

binding.Path <- PropertyPath(sprintf "[%d]" i) 

をしようとするだろう(例えば、これを読ん基づいて、)引用符で囲まれた文字列の中に置く:この採用

http://msdn.microsoft.com/en-us/library/cc645024%28VS.95%29.aspx

+0

残念ながら、うまくいきませんでした。私はそのページを昨日早く読んで、似たようなことをしました(私は列車に乗るためにあなたのポストを詳細に見ていませんでした)。それは四分の一の大きさについて3×4の小さなグリッドを表示します。そこにはテキストはありません。そのクラス(プロパティパス)のドキュメントやソースがあれば良いですが、それはうまくいくでしょうが、少なくともF#とDataGridバインディングのIndexersへのバインディングではうまくいかないようです。 – akaphenom

関連する問題