2017-04-05 69 views
-1

我正嘗試使用WinRT_XamlToolkit_Chart庫從UWP程序的DataTemplate中加載圖表。我使用{Binding Property}語法綁定圖表標題和數據,但不會將該屬性作爲圖表對象的數據成員加載。我既包括我的XAML將數據綁定到UWP WinRT_XamlToolKit_Chart

<Charting:Chart HorizontalAlignment="Center" 
       VerticalAlignment="Center" 
       Width="600" 
       Height="400" 
       DataContext="{Binding}" 
       Title="{Binding Title}"> 
    <Charting:LineSeries Title="{Binding Title}" 
         Margin="0" 
         IndependentValuePath="Name" 
         DependentValuePath="Amount" 
         IsSelectionEnabled="True" 
         ItemsSource="{Binding Data}" 
         /> 
</Charting:Chart> 

C#對象

public class DataChartNode : ExperimentNode 
{ 
    public DataChartNode(String title, String type) 
    { 
     Type = type; 
     Title = title; 
     Category = "Data Analysis"; 
    } 

    public DataChartNode(String type) 
    { 
     Type = type; 
     Category = "Data Analysis"; 
     Name = type; 
     Title = "Hello"; 
     Length = 0; 
     Data = new List<DataPoint>(); 
    } 

    public DataChartNode() { } 

    public string Type { set; get; } 
    public string Title { set; get; } 
    public int Length { set; get; } 
    public List<DataPoint> Data { set; get; } 
} 

回答

0

既然你沒有提供DataPoint類,並沒有告訴你如何建立一個綁定到Chart控件的數據源,我寫了一個簡單的演示,創建了數據源代碼,可以在您可以參考的XAML代碼片段中很好地工作。

XAML代碼(與你同)

<Charting:Chart 
    Title="{Binding Title}" 
    Width="600" 
    Height="400" 
    HorizontalAlignment="Center" 
    VerticalAlignment="Center" 
    DataContext="{Binding}"> 
    <Charting:LineSeries 
     Title="Title" 
     Margin="0" 
     DependentValuePath="Amount" 
     IndependentValuePath="Name" 
     IsSelectionEnabled="True" 
     ItemsSource="{Binding Data}" /> 
</Charting:Chart> 

代碼背後

public sealed partial class MainPage : Page 
{ 
    private Random _random = new Random(); 
    public MainPage() 
    { 
     this.InitializeComponent(); 
     var data = new List<DataPoint>();   
     for (int i = 0; i < 3; i++) 
     { 
      data.Add(new DataPoint { Name = "Name" + i, Amount = _random.Next(10, 100) });    
     } 
     DataChartNode charnode = new DataChartNode() 
     { 
      Title = "Hello", 
      Data = data 
     };   
     this.DataContext = charnode;  
    } 
} 
public class DataChartNode 
{ 
    public string Type { set; get; } 
    public string Title { set; get; } 
    public int Length { set; get; } 
    public List<DataPoint> Data { set; get; } 
} 
public class DataPoint 
{ 
    public string Name { get; set; } 
    public int Amount { get; set; }  
} 

請檢查你的代碼背後發現如果有什麼不妥的地方。