簡體   English   中英

Silverlight圖表嘗試將值綁定到集合而不是元素上

[英]Silverlight charts attempt to bind values to the collection instead of the elements

我的視圖模型上有一個屬性,該屬性返回一些硬編碼的測試數據:

public ObservableCollection<Item> Items
{
  get
  {
    return new ObservableCollection<Item> {
      new Item { Key = 0, Count = 5 },
      new Item { Key = 1, Count = 3 },
      new Item { Key = 2, Count = 2 },
      new Item { Key = 3, Count = 7 }
    }
  }
}

我的圖表在視圖中定義為:

<toolkit:WrapPanel>
  <toolkit:Chart>
    <toolkit:LineSeries ItemsSource="{Binding Items}" IndependentAxis="{Binding Key}" DependentValueBinding="{Binding Count}"/>
  </toolkit:Chart>
</toolkit:WrapPanel>

使用該代碼,我得到以下異常:

System.Windows.Data Error: BindingExpression path error: 'Key' property not found on 'Test.MyViewModel' 'Test.MyViewModel' (HashCode=50157845). BindingExpression: Path='Key' DataItem='Test.MyViewModel' (HashCode=50157845); target element is 'System.Windows.Controls.DataVisualization.Charting.LineSeries' (Name=''); target property is 'IndependentAxis' (type 'System.Windows.Controls.DataVisualization.Charting.IAxis').

如果將圖表的DataContext綁定到Items ,則不會引發任何異常,但圖表不會顯示任何數據。

編輯:不,它抱怨無法綁定鍵和計數在集合上。

我見過的所有示例都按如下方式設置了圖表,那么為什么我的嘗試綁定到視圖模型? 或者更確切地說,這似乎對其他任何人都有效?

您可能需要使用IndependentValueBinding="{Binding Key}"

IndependentAxis指定要使用的軸類型-分類,日期/時間等,而不是從何處獲取軸數據:

<charting:LineSeries.IndependentAxis>
   <charting:CategoryAxis Orientation="X"/>
</charting:LineSeries.IndependentAxis>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM