A191919
A191919

Reputation: 3452

OxyPlot add PlotModel

I am trying to add OxyPlot Chart dynamical, But it doesn't work.

<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>

Code look's like this. FourButton and EightButton doesn't work.

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();

    }
}

} Why chart isn't displaying? If i adding button's it work's fine. I also tryed to use template, but there was no effect.

Upvotes: 3

Views: 3689

Answers (1)

Michał Komorowski
Michał Komorowski

Reputation: 6238

To display a plot you need 2 things:

  • A model
  • A control that will know how to display this model

In your code behind I see that you create and populate PlotModel. However, I don't see any PlotView, that knows how to render PlotModel, in your XAML. Try to add following or similar code to ItemsControl in XAML (I didn't test it though):

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

You should also define waht is oxy e.g.: xmlns:oxy="http://oxyplot.org/wpf

See also this example for reference.

Upvotes: 2

Related Questions