OxyPlot 添加 PlotModel

OxyPlot add PlotModel

我正在尝试动态添加 OxyPlot 图表,但它不起作用。

<Window x:Class="WpfApplication8.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid >
    <Grid.RowDefinitions>
        <RowDefinition Height="20"></RowDefinition>
        <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
    </Grid.ColumnDefinitions>
    <ItemsControl ItemsSource="{Binding TheList}" x:Name="MyGrid" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Height="Auto" Grid.Row="1">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <UniformGrid Rows="{Binding Path=Rows,Mode=TwoWay}" Columns="{Binding Path=Columns,Mode=TwoWay}"></UniformGrid>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>
    <StackPanel Orientation="Horizontal">
        <Button Command="{Binding One}">1</Button>
        <Button Command="{Binding Four}">4</Button>
        <Button Command="{Binding Eight}">8</Button>
    </StackPanel>
</Grid>

代码如下所示。 FourButton 和 EightButton 不起作用。

using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;
using OxyPlot.Wpf;
using OxyPlot;
namespace WpfApplication8
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
/// 

public partial class MainWindow : Window
{

    public class ViewModelTest : INotifyPropertyChanged
    {
        private PlotModel plotModel;
        public PlotModel PlotModel
        {
            get { return plotModel; }
            set { plotModel = value; NotifyPropertyChanged("PlotModel"); }
        }

        private ObservableCollection<PlotModel> theList;
        public ObservableCollection<PlotModel> TheList
        {
            get { return theList; }
            set { theList = value; NotifyPropertyChanged("TheList"); }
        }
        private int rows;
        public int Rows
        {
            get { return rows; }
            set { rows = value; NotifyPropertyChanged("Rows"); }
        }
        private int columns;
        public int Columns
        {
            get { return columns; }
            set { columns = value; NotifyPropertyChanged("Columns"); }
        }
        public ViewModelTest()
        {
            PlotModel = new PlotModel();
            PlotModel.LegendTitle = "Legend";
            PlotModel.LegendOrientation = LegendOrientation.Horizontal;
            PlotModel.LegendPlacement = LegendPlacement.Outside;
            PlotModel.LegendPosition = LegendPosition.TopRight;
            PlotModel.LegendBackground = OxyColor.FromAColor(200, OxyColors.White);
            PlotModel.LegendBorder = OxyColors.Black;

            TheList = new ObservableCollection<PlotModel>();
            One = new RelayCommand(() => OneButton());
            Four = new RelayCommand(() => FourButton());
            Eight = new RelayCommand(() => EightButton());
        }
        public ICommand One { get; set; }
        public ICommand Four { get; set; }
        public ICommand Eight { get; set; }


        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        public void OneButton()
        {
            Rows = 1;
            Columns = 1;
            TheList.Clear();
            for (int i = 0; i < 1; i++)
            {
                TheList.Add(PlotModel);
            }
        }
        public void FourButton()
        {
            Rows = 2;
            Columns = 2;
            TheList.Clear();
            for (int i = 0; i < 4; i++)
            {
                System.Windows.Controls.Button newBtn = new Button();
                newBtn.Content = i.ToString();
                newBtn.Name = "Button" + i.ToString();
                //TheList.Add(newBtn);
            }
        }
        public void EightButton()
        {
            Rows = 2;
            Columns = 4;
            TheList.Clear();
            for (int i = 0; i < 8; i++)
            {
                System.Windows.Controls.Button newBtn = new Button();
                newBtn.Content = i.ToString();
                newBtn.Name = "Button" + i.ToString();
                //TheList.Add(newBtn);
            }
        }
    }
    public MainWindow()
    {

        DataContext = new ViewModelTest();
        InitializeComponent();

    }
}

} 为什么图表不显示?如果我添加按钮,它工作正常。我也尝试过使用模板,但是没有效果。

要显示情节,您需要两件事:

  • 一个模型
  • 知道如何显示此模型的控件

在您后面的代码中,我看到您创建并填充了 PlotModel。但是,我没有在您的 XAML 中看到任何知道如何呈现 PlotModelPlotView。尝试向 XAML 中的 ItemsControl 添加以下或类似代码(虽然我没有测试):

<ItemsControl.ItemTemplate>
   <DataTemplate>
      <oxy:PlotView Model="{Binding}"/>
   </DataTemplate>
</ItemsControl.ItemTemplate>

您还应该定义什么是 oxy 例如:xmlns:oxy="http://oxyplot.org/wpf

另请参阅 this 示例以供参考。