简体   繁体   中英

In WPF, how do I copy a binding from a TextBlock to custom collection?

Problem: I have a ListBox with TextBlocks whos Text property are bound to different properties. I wish to drag the TextBlock onto a OxyPlot and have the plot create a new LineSeries with a collection that should be bound to the same binding as for the TextBlock (is this making sense?)

I have derived a class from TextBlock to handle the OnMouseMove event like this:

    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);
        }
    }

Also I have derived a class from Oxy.Plot that handles the 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;
    }

The AddSeries function does the following:

public void AddSeries(BindingExpression binding)
    {
        plot1 = new PlotCollection();

        LineSeries newSeries = new LineSeries();
        newSeries.ItemsSource = plot1.Collection;

        Series.Add(newSeries);
    }

And lastly the PlotCollection is defined as:

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));
    }
}

So my question is this: How do I create a binding on PlotCollection.Value that matches the one from the TextBlock.Text?

在您的AddSeries方法中,尝试添加以下代码行:

BindingOperations.SetBinding(plot1, PlotCollection.ValueProperty, binding.ParentBinding);

Found out the problem,

I needed to add a PropertyChangedCallback to the ValueProperty declaration, like this:

public static DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(double), typeof(DynamicSeries), new PropertyMetadata(0.0, OnValueChanged));

And then handle the property changes in the callback method:

private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        PlotCollection ds = (PlotCollection)d;
        ds.AppendValue((double)e.NewValue);
    }

I guess that I have misunderstood how the Value property works?!

Thanks for taking the time to try and help me...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM