我创建了WPF应用程序,添加了图表标记,进行了绑定。 当我向图表数据添加新点时,轴会更改其最大值,但图形线不会显示。
我无法理解错误。
这是我的图表的标记:
<chartingToolkit:Chart x:Name="chart"
BorderThickness="1"
VerticalAlignment="Stretch"
Grid.Row="1">
<chartingToolkit:LineSeries Title="Options Count"
Visibility="Visible"
Background="Transparent"
ItemsSource="{Binding MyGraph}"
IndependentValuePath="Key"
DependentValuePath="Value"
DataPointStyle="{StaticResource DataPointStyle1}"
/>
<chartingToolkit:Chart.Axes>
<chartingToolkit:LinearAxis Orientation="Y"
Minimum="0"
Maximum="{Binding MaxValueForAxis}"
ShowGridLines="True" />
<chartingToolkit:DateTimeAxis Orientation="X"
ShowGridLines="True"
Minimum="{Binding MinDateForAxis}"
Maximum="{Binding MaxDateForAxis}"
IntervalType="Auto"
AxisLabelStyle="{StaticResource AxisStyle}"/>
</chartingToolkit:Chart.Axes>
</chartingToolkit:Chart>
添加值的ViewModel代码
public void AddNew()
{
MyGraph.Add(DateTime.Now.AddDays(i), i * 100);
i++;
OnPropertyChanged("MyGraph");
OnPropertyChanged("MaxValueForAxis");
OnPropertyChanged("MinDateForAxis");
OnPropertyChanged("MaxDateForAxis");
}
以下是我设置DataContext的方法
ViewModel vm = new ViewModel();
public MainWindow()
{
InitializeComponent();
DataContext = vm;
}
答案 0 :(得分:1)
在视图模型中使用ObservableCollection<KeyValuePair<DateTime,int>>
,原因是此集合已实现INotifyCollectionChanged
和INotifyPropertyChanged
,而Dictionary<>
没有。
<强> XAML:强>
<Window x:Class="WpfApplication339.MainWindow"
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:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
xmlns:local="clr-namespace:WpfApplication339"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="10*"></RowDefinition>
</Grid.RowDefinitions>
<Button Content="Add" Grid.Row="0" Click="Button_Click" Width="75" HorizontalAlignment="Left"/>
<chartingToolkit:Chart x:Name="chart"
BorderThickness="1"
VerticalAlignment="Stretch"
Grid.Row="1">
<chartingToolkit:LineSeries Title="Options Count"
Visibility="Visible"
Background="Transparent"
ItemsSource="{Binding MyGraph}"
IndependentValuePath="Key"
DependentValuePath="Value"
/>
<chartingToolkit:Chart.Axes>
<chartingToolkit:LinearAxis Orientation="Y"
Minimum="0"
Maximum="{Binding MaxValueForAxis}"
ShowGridLines="True" />
<chartingToolkit:DateTimeAxis Orientation="X"
ShowGridLines="True"
Minimum="{Binding MinimumDateForAxis}"
Maximum="{Binding MaxDateForAxis}"
IntervalType="Auto"
/>
</chartingToolkit:Chart.Axes>
</chartingToolkit:Chart>
</Grid>
</Window>
<强>主窗口:强>
public partial class MainWindow : Window
{
MyViewModel vm;
int i;
public MainWindow()
{
InitializeComponent();
vm = new MyViewModel();
DataContext = vm;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
vm.MyGraph.Add(new KeyValuePair<DateTime, int>(DateTime.Now.AddDays(i), i * 100));
i++;
}
}
<强>视图模型:强>
public class MyViewModel
{
public ObservableCollection<KeyValuePair<DateTime,int>> MyGraph { get; set; }
public MyViewModel()
{
MyGraph = new ObservableCollection<KeyValuePair<DateTime, int>>();
}
}