我正在使用WPF应用。此应用使用OxyPlot chart control。我让用户进行计算。用户可以以网格和图表形式查看结果。为了做到这一点,我有以下内容:
home.xaml
<DataGrid x:Name="resultDataGrid" ItemsSource="{Binding Items}">
<DataGrid.Columns>
<DataGridTextColumn Header="#" Binding="{Binding Number}" />
<DataGridTextColumn Header="Description" Binding="{Binding Description}" />
</DataGrid.Columns>
</DataGrid>
<oxy:Plot x:Name="resultGraph" Background="Transparent" Height="400" HorizontalContentAlignment="Stretch" VerticalAlignment="Stretch" Visibility="Collapsed">
<oxy:Plot.Series>
<oxy:LineSeries ItemsSource="{Binding Points}" MarkerType="Circle" Title="Result" />
</oxy:Plot.Series>
</oxy:Plot>
<StackPanel Orientation="Horizontal">
<Button x:Name="executeButton" Click="executeButton_Click" />
<Button x:Name="viewData" Click="viewData_Click" />
<Button x:Name="viewChart" Click="viewChart_Click" />
</StackPanel>
home.xaml.cs
public partial class Home: Page
{
private MyViewModel viewModel = null;
public Home()
{
InitializeComponent();
viewModel = new MyViewModel();
this.DataContext = viewModel;
}
private void executeButton_Click(object sender, RoutedEventArgs e) {
viewModel.Execute();
}
private void viewData_Click(object sender, RoutedEventArgs e)
{
resultDataGrid.Visibility = System.Windows.Visibility.Visible;
resultGraph.Visibility = System.Windows.Visibility.Collapsed;
}
private void viewChart_Click(object sender, RoutedEventArgs e)
{
resultDataGrid.Visibility = System.Windows.Visibility.Collapsed;
resultGraph.Visibility = System.Windows.Visibility.Visible;
}
}
MyViewModel.cs
public class MyViewModel : INotifyPropertyChanged
{
public Dictionary<int, string> Items{ get; set; }
public IList<DataPoint> Points{ get; set; }
public void Execute()
{
this.Items = new Dictionary<int, string>();
this.Points = new List<DataPoint>();
var randomNumber = GetRandomNumber();
for (var i=0; i<10; i++)
{
this.Items.Add((i+1), "Item #" + i);
var dataPoint = new DataPoint(i, (randomNumber*i));
this.Points.Add(dataPoint);
}
NotifyPropertyChanged("Items");
NotifyPropertyChanged("Points");
}
}
我的问题是,当用户点击&#34;执行&#34;按钮,数据在DataGrid
中刷新,这正是我所期待的。但是,如果图表首次可见,则只会刷新图表。换句话说,如果我点击&#34;执行&#34;当DataGrid
可见时,图表中的绘图点变得陈旧。但是,如果我有resultGraph
可见,我点击&#34;执行&#34;点数按预期刷新。我不明白我做错了什么。任何帮助表示赞赏。
答案 0 :(得分:2)
在ViewModel中创建
private int _invalidateFlag = 0;
public int InvalidateFlag
{
get {return _invalidateFlag;}
set
{
_invalidateFlag = value;
NotifyPropertyChanged();
}
}
//and to the end of Execute() add
InvalidateFlag++;
也不要忘记绑定InvalidateFlag
<oxy:Plot x:Name="resultGraph" InvalidateFlag="{Binding InvalidateFlag}" Background="Transparent" Height="400" HorizontalContentAlignment="Stretch" VerticalAlignment="Stretch" Visibility="Collapsed">
这应该可以解决问题
答案 1 :(得分:1)
其实我找到了答案here:
OxyPlot不会观察属性或集合更改。这意味着客户端应用程序负责在内容更改时刷新绘图。
更改后必须使绘图无效。有几个possibilities。
最简单的可能是 dirty MVVM:在视图中订阅viewmodel PropertyChanged
事件,检查Points
上升的此事件是否已更改并使绘图无效。
答案 2 :(得分:1)
通常,OxyPlot提供方法PlotModel.InvalidatePlot(true)
以使用新输入数据刷新图表。
编辑:哦,我看到你的ViewModel中没有PlotModel属性。我的回答需要这样一个属性,它也可以绑定到xaml。