gpt4 book ai didi

c# - OxyPlot 添加 PlotModel

转载 作者:太空宇宙 更新时间:2023-11-03 13:10:04 29 4
gpt4 key购买 nike

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

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

另见 this示例供引用。

关于c# - OxyPlot 添加 PlotModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29277481/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com