2017-10-17 33 views
0

綁定圖表所以我想結合我的Chart裏面ComboBox裏面的ComboBox

XAML: 

<ComboBox x:Name="cbAdapters" SelectedIndex="0" Margin="30"> 
    <ComboBox.ItemTemplate> 
     <DataTemplate> 
      <telerik:RadCartesianChart x:Name="chartTemplate" 
             Width="300" Height="200"> 
       <telerik:RadCartesianChart.VerticalAxis> 
        <telerik:LinearAxis /> 
       </telerik:RadCartesianChart.VerticalAxis> 
       <telerik:RadCartesianChart.HorizontalAxis> 
        <telerik:CategoricalAxis/> 
       </telerik:RadCartesianChart.HorizontalAxis> 
       <telerik:LineSeries ValueBinding="Rate" CategoryBinding="Category" ItemsSource="{Binding ChartPlotInfos}" /> 
      </telerik:RadCartesianChart> 
     </DataTemplate> 
    </ComboBox.ItemTemplate> 
</ComboBox> 

Code behind

public MainWindow() 
{ 
    InitializeComponent(); 

    var r = new Random(); 
    var comboBoxSource = new ObservableCollection<MachineNetworkAdapter>(); 
    for (int i = 0; i < 3; i++) 
    { 
     var adapter = new MachineNetworkAdapter() 
     { 
      Name = "Adapter " + i, 
      IpAddress = r.Next(0, 255) + "." + r.Next(0, 255) + "." + r.Next(0, 255) + "." + r.Next(0, 255), 
      ChartPlotInfos = new ObservableCollection<ChartPlotInfo>(), 
     }; 

     for (int k = 0; k < 10; k++) 
     { 
      adapter.ChartPlotInfos.Add(new ChartPlotInfo() { Category = "Category " + k, Rate = r.Next(100, 300) }); 
     } 

     comboBoxSource.Add(adapter); 
    } 

    cbAdapters.ItemsSource = comboBoxSource; 
} 

}

型號:

public class MachineNetworkAdapter 
{ 
    public string Name { get; set; } 
    public string IpAddress { get; set; } 
    public ObservableCollection<ChartPlotInfo> ChartPlotInfos { get; set; } 
} 

public class ChartPlotInfo 
{ 
    public double Rate { get; set; } 
    public string Category { get; set; } 
} 

所以這工作得很好,除了這個,在我的ComboBox裏面,我只能看到我的Chart,但我的MachineNetworkAdapterNameIP Address丟失。

+0

在'DataTemplate',你有沒有爲'Name'和'IpAddress'結合。 – Iron

回答

0

您應該放置兩個控件來顯示NameIpAddress,並且不要忘記爲它們設置綁定。

<ComboBox x:Name="cbAdapters" SelectedIndex="0" Margin="30"> 
    <ComboBox.ItemTemplate> 
     <DataTemplate> 
      <StackPanel Orientation="Horizontal"> 
       <TextBlock Text="{Binding Name}"/> 
       <TextBlcok Text="{Binding IpAddress}"/> 
       <telerik:RadCartesianChart x:Name="chartTemplate" Width="300" Height="200"> 
        <telerik:RadCartesianChart.VerticalAxis> 
         <telerik:LinearAxis /> 
        </telerik:RadCartesianChart.VerticalAxis> 
        <telerik:RadCartesianChart.HorizontalAxis> 
         <telerik:CategoricalAxis/> 
        </telerik:RadCartesianChart.HorizontalAxis> 
        <telerik:LineSeries ValueBinding="Rate" CategoryBinding="Category" ItemsSource="{Binding ChartPlotInfos}" /> 
       </telerik:RadCartesianChart> 
      </StackPanel> 
     </DataTemplate> 
    </ComboBox.ItemTemplate> 
</ComboBox>