【问题标题】:OxyPlot add PlotModelOxyPlot 添加 PlotModel
【发布时间】:2015-05-30 09:52:41
【问题描述】:

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

    }
}

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

【问题讨论】:

    标签: c# wpf oxyplot


    【解决方案1】:

    要显示一个情节,你需要两件事:

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

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

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

    您还应该定义 waht is oxy 例如:xmlns:oxy="http://oxyplot.org/wpf

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

    【讨论】:

    • 它不会像我想要的那样工作。因为如果我使用模板,就像你 adwise 一样。它将只有一个图表。我需要在后面的代码中生成 1-8 个图表。我不想更改代码中的模板。主要问题是将 Oxyplot 添加到 UniformGrid。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 2014-05-30
    • 1970-01-01
    相关资源
    最近更新 更多