0

私の目標は、2 秒ごとにポイントを追加できる Polyline を持つことであり、PolyLine はそれを UI に反映します。

次のように、値コンバーターを使用して ObservableCollection にバインドされた PolyLine があります。

XAML

<Polyline Points="{Binding OutCollection,Mode=TwoWay,Converter={StaticResource pointConverter}}" Stroke="Blue" StrokeThickness="15" Name="Line2"  />

値コンバーター

 public class PointCollectionConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        PointCollection outCollection = new PointCollection();
        if (value is ObservableCollection<Point>)
        {
            var inCollection = value as ObservableCollection<Point>;

            foreach (var item in inCollection)
            {
                outCollection.Add(new Point(item.X, item.Y));
            }
            return outCollection;
        }
        else
            throw new Exception("Wrong Input");
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new Exception("Wrong Operation");
    }
}

そしてコードビハインドファイルで:

public MainWindow()
    {
        InitializeComponent();
        this.DataContext = vm;
        InitTimer();
    }

    private void InitTimer()
    {
        dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
        dispatcherTimer.Tick += new EventHandler(TickHandler);
        dispatcherTimer.Interval = new TimeSpan(0, 0, 2);
        dispatcherTimer.Start();
    }
    int counter = 0;
    private void TickHandler(object sender, EventArgs e)
    {
        double val = (counter * counter) * (0.001);
        var point = new Point(counter, val);
        vm.OutCollection.Add(point);
        vm.OutCollection.CollectionChanged += OutCollection_CollectionChanged;         

        counter++;
    }

ビューモデル:

public class ViewModel : ViewModelBase
{
    public ObservableCollection<Point> OutCollection
    {
        get
        {
            return m_OutCollection;
        }

        set
        {
            m_OutCollection = value;
            this.OnPropertyChanged("OutCollection");
        }
    }

}

    private ObservableCollection<Point> m_OutCollection;


    public ViewModel()
    {

        OutCollection = new ObservableCollection<Point>();

    }

動いていない。何か助けていただければ幸いです?!?

4

1 に答える 1

0

私はそれを自分で解決しました。

ObservableCollection は役に立たないかもしれませんが、助けになったのは追加することです

Line2.GetBindingExpression(Polyline.PointsProperty).UpdateTarget();

の中にTickHandler()

于 2016-05-04T19:09:51.380 に答える