gpt4 book ai didi

wpf - 刷新 OxyPlot 图表

转载 作者:行者123 更新时间:2023-12-01 00:44:37 28 4
gpt4 key购买 nike

我正在开发一个 WPF 应用程序。此应用程序使用 OxyPlot chart control .我让用户执行计算。用户可以以网格和图表的形式查看结果。为了做到这一点,我有以下几点:

主页.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>

主页.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");
}
}

我的问题是,当用户单击“执行”按钮时, DataGrid 中的数据会被刷新。 ,这是我所期待的。但是,仅当图表首次可见时才会刷新图表。换句话说,如果我在 DataGrid 时单击“执行”可见,图表中的绘图点变得陈旧。但是,如果我有 resultGraph可见,我单击“执行”点按预期刷新。我不明白我做错了什么。任何帮助表示赞赏。

最佳答案

在您的 ViewModel 中创建

private int _invalidateFlag = 0;
public int InvalidateFlag
{
get {return _invalidateFlag;}
set
{
_invalidateFlag = value;
NotifyPropertyChanged();
}
}

//and to the end of Execute() add

InvalidateFlag++;

也不要忘记绑定(bind) InvalidateFlag
<oxy:Plot x:Name="resultGraph" InvalidateFlag="{Binding InvalidateFlag}" Background="Transparent" Height="400" HorizontalContentAlignment="Stretch" VerticalAlignment="Stretch" Visibility="Collapsed">

这应该可以解决问题

关于wpf - 刷新 OxyPlot 图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35434032/

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