无法使用 MVVM 将 WPF ChartPlotter 绑定到视图
Unable to bind the WPF ChartPlotter to the View using MVVM
我刚刚关注了 to create a simple Chart Plotter using MVVM. But I am unable to bind the data-source Output image。
XAML
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:GrayPlotter"
xmlns:d3="http://research.microsoft.com/DynamicDataDisplay/1.0" x:Class="GrayPlotter.MainWindow"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="117*"/>
<ColumnDefinition Width="192*"/>
<ColumnDefinition Width="143*"/>
<ColumnDefinition Width="58*"/>
<ColumnDefinition Width="7*"/>
</Grid.ColumnDefinitions>
<d3:ChartPlotter x:Name="chartplot" Grid.ColumnSpan="3" HorizontalAlignment="Left" Height="168" Margin="43,65,0,0" VerticalAlignment="Top" Width="379">
<d3:LineGraph DataSource="{Binding Plotvalue}"></d3:LineGraph>
</d3:ChartPlotter>
</Grid>
MainWindowViewModel.cs
namespace GrayPlotter.ViewModel
{
class MainWindowViewModel : ObservableObject
{
ChartPlotter Plotvaluetemp = new ChartPlotter();
ChartPlotter plotvalue = null;
public ChartPlotter Plotvalue
{
get { return plotvalue; }
set
{
plotvalue = value;
RaisePropertyChangedEvent("Plotvalue");
}
}
class plotData
{
public ObservableDataSource<Point> Data { get; set; }
public plotData()
{
Data = new ObservableDataSource<Point>();
}
}
plotData plotDatavalue = new plotData();
public void UpdateplotInformation()
{
double[] my_array = new double[10];
for (int i = 0; i < my_array.Length; i++)
{
my_array[i] = Math.Sin(i);
plotDatavalue.Data.Collection.Add(new Point(i, my_array[i]));
}
Plotvaluetemp.AddLineGraph(plotDatavalue.Data);
Plotvalue = Plotvaluetemp;
}
}
}
和Mainwindow.xaml.cs
namespace GrayPlotter
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
///
public partial class MainWindow : Window
{
MainWindowViewModel viewModel ;
public MainWindow()
{
InitializeComponent();
viewModel = new MainWindowViewModel();
this.DataContext = viewModel;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
viewModel.UpdateplotInformation();
}
}
}
我就在附近,但我错过了什么?
尝试改变你的 xaml =>
DataSource="{Binding Data}"
然后你回到 =>
private ObservableDataSource<Point> data;
public ObservableDataSource<Point> Data
{
get { return data; }
set
{
data = value;
RaisPropertyChangedEvent("Data");
}
}
public void UpdateplotInformation()
{
var list = new List<Point>();
for (int i = 0; i < 10; i++)
{
list.Add(new Point(i, Math.Sin(i)));
}
Data = new ObservableDataSource<Point>(list);
}
我刚刚关注了
XAML
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:GrayPlotter"
xmlns:d3="http://research.microsoft.com/DynamicDataDisplay/1.0" x:Class="GrayPlotter.MainWindow"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="117*"/>
<ColumnDefinition Width="192*"/>
<ColumnDefinition Width="143*"/>
<ColumnDefinition Width="58*"/>
<ColumnDefinition Width="7*"/>
</Grid.ColumnDefinitions>
<d3:ChartPlotter x:Name="chartplot" Grid.ColumnSpan="3" HorizontalAlignment="Left" Height="168" Margin="43,65,0,0" VerticalAlignment="Top" Width="379">
<d3:LineGraph DataSource="{Binding Plotvalue}"></d3:LineGraph>
</d3:ChartPlotter>
</Grid>
MainWindowViewModel.cs
namespace GrayPlotter.ViewModel
{
class MainWindowViewModel : ObservableObject
{
ChartPlotter Plotvaluetemp = new ChartPlotter();
ChartPlotter plotvalue = null;
public ChartPlotter Plotvalue
{
get { return plotvalue; }
set
{
plotvalue = value;
RaisePropertyChangedEvent("Plotvalue");
}
}
class plotData
{
public ObservableDataSource<Point> Data { get; set; }
public plotData()
{
Data = new ObservableDataSource<Point>();
}
}
plotData plotDatavalue = new plotData();
public void UpdateplotInformation()
{
double[] my_array = new double[10];
for (int i = 0; i < my_array.Length; i++)
{
my_array[i] = Math.Sin(i);
plotDatavalue.Data.Collection.Add(new Point(i, my_array[i]));
}
Plotvaluetemp.AddLineGraph(plotDatavalue.Data);
Plotvalue = Plotvaluetemp;
}
}
}
和Mainwindow.xaml.cs
namespace GrayPlotter
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
///
public partial class MainWindow : Window
{
MainWindowViewModel viewModel ;
public MainWindow()
{
InitializeComponent();
viewModel = new MainWindowViewModel();
this.DataContext = viewModel;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
viewModel.UpdateplotInformation();
}
}
}
我就在附近,但我错过了什么?
尝试改变你的 xaml =>
DataSource="{Binding Data}"
然后你回到 =>
private ObservableDataSource<Point> data;
public ObservableDataSource<Point> Data
{
get { return data; }
set
{
data = value;
RaisPropertyChangedEvent("Data");
}
}
public void UpdateplotInformation()
{
var list = new List<Point>();
for (int i = 0; i < 10; i++)
{
list.Add(new Point(i, Math.Sin(i)));
}
Data = new ObservableDataSource<Point>(list);
}