我正在使用MVVM模式创建WPF SciChart应用程序(仍在学习中)。 在此应用程序中,我正在创建一个编辑功能,其中用户在屏幕上绘制一个框,并将两行添加到图表viewModel中类型为IAnnotationViewModel的ObservableCollection中。
然后我有一个修改器类,该类继承了ChartModifierBase(访问数据系列,轴等)。在此类中,我创建了一个依赖项属性,该属性将2种方式绑定到图表viewModel中的ObservableCollection。将线添加到图表表面时,dp会更新,但是图表viewModel中的集合不是。我是否缺少某些东西...我希望如此,以便我不确定如何实现PropertyChangedCallback委托。请参见下面的代码
<s:SciChartSurface Annotations="{s:AnnotationsBinding Annotations}"
s:SciChartGroup.VerticalChartGroup="myGroup"
Padding="5"
x:Name="ChartTwo"
ZoomHistoryManager="{StaticResource ZoomHistoryManager}"
RenderableSeries="{s:SeriesBinding ChartTwo,UpdateSourceTrigger=PropertyChanged}">
<s:SciChartSurface.ChartModifier>
<s:ModifierGroup s:MouseManager.MouseEventGroup="myCustomGroup">
<modifiers:SelectionModifier IsEnabled="{Binding ElementName=SelectionModifierToggle ,Path=IsChecked}"
Annotations="{Binding Path = Annotations, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
SelectionPolygonStyle="{StaticResource SelectionStyle}"/>
</s:ModifierGroup>
</s:SciChartSurface.ChartModifier>
</s:SciChartSurface>
public class ChartEditViewModel
{
private ObservableCollection<IAnnotationViewModel> _annotations = new ObservableCollection<IAnnotationViewModel>();
public ObservableCollection<IAnnotationViewModel> Annotations
{
get { return _annotations; }
set { _annotations = value; OnPropertyChanged("Annotations"); }
}
}
public class VerticalLineViewModel : VerticalLineAnnotationViewModel
{
public override Type ViewType
{
get { return typeof(VerticalLineAnnotation); }
}
}
public class SelectionModifier : ChartModifierBase, IDialogRequestClose
{
private IList<IPointMetadata> _seriesPointMetadata; //selected series metadata
MoveBlockViewModel moveBlockViewModel;
IDialogService dialogService;
public ObservableCollection<IAnnotationViewModel> Annotations
{
get { return (ObservableCollection<IAnnotationViewModel>)GetValue(AnnotationsProperty); }
set { SetValue(AnnotationsProperty, value); }
}
// Using a DependencyProperty as the backing store for Annotations. This enables animation, styling, binding, etc...
public static readonly DependencyProperty AnnotationsProperty =
DependencyProperty.Register("Annotations", typeof(ObservableCollection<IAnnotationViewModel>), typeof(SelectionModifier), new PropertyMetadata());
public Style SelectionPolygonStyle
{
get { return (Style)GetValue(SelectionPolygonStyleProperty); }
set { SetValue(SelectionPolygonStyleProperty, value); }
}
// Using a DependencyProperty as the backing store for SelectionPolygonStyle. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SelectionPolygonStyleProperty =
DependencyProperty.Register("SelectionPolygonStyle", typeof(Style), typeof(SelectionModifier), new PropertyMetadata(default(Style)));
///<summary>
///reticule
///</summary>
private Rectangle _rectangle;
private bool _isDragging;
private Point _startPoint;
private Point _endPoint;
public VerticalLineViewModel Line1 = new VerticalLineViewModel();
public VerticalLineViewModel Line2 { get; set; }
private IRenderableSeries _selectedSeries;
public event EventHandler<DialogCloseRequestedEventArgs> CloseRequested;
public SelectionModifier()
{
}
public override void OnModifierMouseUp(ModifierMouseArgs e)
{
if (!_isDragging) return;
base.OnModifierMouseUp(e);
var ptTrans = GetPointRelativeTo(e.MousePoint, ModifierSurface);
_endPoint = SetReticulePosition(_rectangle, _startPoint, ptTrans, e.IsMaster);
double distanceDragged = PointUtil.Distance(_startPoint, ptTrans);
if (distanceDragged > 10.0)
{
e.Handled = true;
}
_isDragging = false;
if (e.IsMaster) ModifierSurface.ReleaseMouseCapture();
var startX = _selectedSeries.XAxis.GetCurrentCoordinateCalculator().GetDataValue(_startPoint.X);
var endX = _selectedSeries.XAxis.GetCurrentCoordinateCalculator().GetDataValue(_endPoint.X);
var startY = _selectedSeries.YAxis.GetCurrentCoordinateCalculator().GetDataValue(_startPoint.Y);
var endY = _selectedSeries.YAxis.GetCurrentCoordinateCalculator().GetDataValue(_endPoint.Y);
//XAxis.AnimateVisibleRangeTo(new DoubleRange(startX, endX),TimeSpan.FromMilliseconds(300));
startX = (int)Math.Round(startX, 1);
endX = (int)Math.Round(endX, 1);
var startIndex = _selectedSeries.DataSeries.XValues.IndexOf(startX);
var endIndex = _selectedSeries.DataSeries.XValues.IndexOf(endX);
//YAxis.AnimateVisibleRangeTo(new DoubleRange(startY, endY),TimeSpan.FromMilliseconds(300));
if (_startPoint != _endPoint)
{
if (startIndex == -1)
startIndex = 0;
if (endIndex == -1)
endIndex = _selectedSeries.DataSeries.XValues.Count - 1;
Line1 = AddVerticalLine((double)_selectedSeries.DataSeries.XValues[startIndex]);
Line2 = AddVerticalLine((double)_selectedSeries.DataSeries.XValues[endIndex]);
X1 = (double)Line1.X1;
Line1.PropertyChanged += Line_PropertyChanged;
Line2.PropertyChanged += Line_PropertyChanged;
Annotations.Add(Line1);
Annotations.Add(Line2);
for (int i = startIndex; i <= endIndex; i++)
{
if (i > -1)
_seriesPointMetadata[i].IsSelected = true;
}
}
ParentSurface.InvalidateElement();
ClearReticule();
}