问题:我有一个带有TextBlocks的ListBox,其中Text属性绑定到不同的属性。我希望将TextBlock拖到OxyPlot上,并让绘图创建一个新的LineSeries,其集合应绑定到与TextBlock相同的绑定(这有意义吗?)
我从TextBlock派生了一个类来处理OnMouseMove事件,如下所示:
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (CanDrag && (e.LeftButton == MouseButtonState.Pressed))
{
// Make sure we have a data binding
BindingExpression binding = GetBindingExpression(TextProperty);
if(binding == null)
{ return; }
// Package the data.
DataObject data = new DataObject();
data.SetData("DragListText.Binding", binding);
// Inititate the drag-and-drop operation.
DragDrop.DoDragDrop(this, data, DragDropEffects.Copy);
}
}
我还从Oxy.Plot派生了一个处理OnDrop的类:
protected override void OnDrop(DragEventArgs e)
{
base.OnDrop(e);
// DataObject must contain a DragListText.Binding object
if (e.Data.GetDataPresent("DragListText.Binding"))
{
BindingExpression binding = e.Data.GetData("DragListText.Binding") as BindingExpression;
AddSeries(binding);
}
e.Handled = true;
}
AddSeries函数执行以下操作:
public void AddSeries(BindingExpression binding)
{
plot1 = new PlotCollection();
LineSeries newSeries = new LineSeries();
newSeries.ItemsSource = plot1.Collection;
Series.Add(newSeries);
}
最后,PlotCollection定义为:
public class PlotCollection : DependencyObject
{
public ObservableCollection<DataPoint> Collection;
public static DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(double), typeof(PlotCollection), new PropertyMetadata(0.0, new PropertyChangedCallback(OnValueChanged)));
private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{ ((PlotCollection)d).AddLast(); }
public double Value
{
get { return (double)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
public PlotCollection()
{
Collection = new ObservableCollection<DataPoint>();
}
protected void AddLast()
{
Collection.Add(new DataPoint(OxyPlot.Axes.DateTimeAxis.ToDouble(DateTime.Now), Value));
}
}
所以我的问题是:如何在PlotCollection.Value上创建一个与TextBlock.Text匹配的绑定?
答案 0 :(得分:0)
在AddSeries方法中,尝试添加以下代码行:
BindingOperations.SetBinding(plot1, PlotCollection.ValueProperty, binding.ParentBinding);
答案 1 :(得分:0)
发现问题,
我需要将PropertyChangedCallback添加到ValueProperty声明中,如下所示:
public static DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(double), typeof(DynamicSeries), new PropertyMetadata(0.0, OnValueChanged));
然后在回调方法中处理属性更改:
private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
PlotCollection ds = (PlotCollection)d;
ds.AppendValue((double)e.NewValue);
}
我想我误解了Value属性是如何工作的?!
感谢您抽出宝贵时间尝试帮助我......