OxyPlot 添加 PlotModel

3
我尝试动态添加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();

    }
}

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

1个回答

2
要显示一个图表,您需要两个东西:
  • 一个模型
  • 一个控件,它将知道如何显示此模型
在您的代码后台中,我看到您创建并填充了PlotModel。但是,在您的XAML中,我没有看到任何知道如何呈现PlotModelPlotView。尝试将以下或类似的代码添加到XAML中的ItemsControl中(虽然我没有测试过):
<ItemsControl.ItemTemplate>
   <DataTemplate>
      <oxy:PlotView Model="{Binding}"/>
   </DataTemplate>
</ItemsControl.ItemTemplate>

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

参考此示例以供参考。


它不会按照我想要的方式工作。因为如果我使用模板,就像你建议的那样。它只会有一个图表。我需要在代码后生成1-8个图表。我不想在代码中更改模板。主要问题是将Oxyplot添加到UniformGrid中。 - A191919

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接