首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >F# Silverlight:使用PropertyPath或其他方法依次绑定列

F# Silverlight:使用PropertyPath或其他方法依次绑定列
EN

Stack Overflow用户
提问于 2010-12-07 21:53:50
回答 2查看 452关注 0票数 1

我试图动态地填充一个自动完成的值。我得到了一个SearchResult元组列表,其中每个元组第一个字符串是键,字符串列表是每个列需要显示的显示文本。假设在任何SearchResult列表中,所有行都将在显示文本列表中包含相同数量的项。我真的希望能够按序号绑定到显示文本列表的值.

一个简化的例子是这样的数据:

代码语言:javascript
复制
 [
     ("MSFT.OQ", ["MSFT.OQ"; "Microsoft"; "Nasdaq"]) ;
     ("GOOG.OQ", ["GOOG.OQ"; "Google"; "Nasdaq"]);
 ]

和展示:

代码语言:javascript
复制
MSFT.OQ         Microsoft       Nasdaq
GOOG.OQ         Google          Nasdaq

但我看到的是:

代码语言:javascript
复制
["MSFT.OQ"; "Microsoft"; "Nasdaq"]     ["MSFT.OQ"; "Microsoft"; "Nasdaq"]    ["MSFT.OQ"; "Microsoft"; "Nasdaq"]
["GOOG.OQ"; "Google"; "Nasdaq"]        ["GOOG.OQ"; "Google"; "Nasdaq"]       ["GOOG.OQ"; "Google"; "Nasdaq"]

整个列表将在每一列中结束,因此我认为我的绑定已经取消。

我的示例代码(试图从更复杂的模型中简化):

代码语言:javascript
复制
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

谢谢..。

令人吃惊的是(至少对我来说)这些结果是一样的:

代码语言:javascript
复制
   binding.Path <- PropertyPath([])
   binding.Path <- PropertyPath("")
   binding.Path <- PropertyPath([0].[0])

对我来说没什么意义。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-12-07 23:02:37

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

工作过:

代码语言:javascript
复制
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()

在我的代码中,将绑定替换为

代码语言:javascript
复制
let binding = System.Windows.Data.Binding()
binding.Path <- PropertyPath([])
binding.Converter <- RowIndexConverter()
binding.ConverterParameter <- i

我想这是个很好的解决方案。希望大脑(是一个错误或神的干预)也能发挥作用,它更直截了当。

票数 0
EN

Stack Overflow用户

发布于 2010-12-07 22:53:38

我不知道,但我会试着

代码语言:javascript
复制
binding.Path <- PropertyPath(sprintf "[%d]" i)

(例如把它放在引用的字符串中),根据以下内容:

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4382013

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档