2017-10-17 31 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>