我试图添加OxyPlot Chart动态,但它不起作用。
<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();
}
}
} 为什么图表不显示?如果我添加按钮,它可以正常工作。我也尝试使用模板,但没有效果。
答案 0 :(得分:1)
要显示情节,您需要两件事:
在您的代码中,我看到您创建并填充PlotModel
。但是,我在XAML中看不到任何知道如何呈现PlotView
的{{1}}。{1}}。尝试在XAML中向PlotModel
添加以下或类似的代码(虽然我没有测试过):
ItemsControl
您还应该定义waht oxy ,例如:<ItemsControl.ItemTemplate>
<DataTemplate>
<oxy:PlotView Model="{Binding}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
另见this示例以供参考。