在ComboBox中绑定图表

时间:2017-10-17 12:46:56

标签: wpf charts combobox binding

所以我想在Alt+F5

中绑定Chart
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 MachineNetworkAdapterName丢失。

1 个答案:

答案 0 :(得分: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>