简体   繁体   English

绑定OxyPlot Tracker并在TextBlock中设置获取值

[英]Binding OxyPlot Tracker and set getting value in TextBlock

Project - WPF, C#, IDE - Visual Studio. 项目-WPF,C#,IDE-Visual Studio。 I want bind value traking on my PlotView . 我想在我的PlotView上绑定值跟踪 My code XAML: 我的代码XAML:

 <Border CornerRadius="6" BorderBrush="Gray" BorderThickness="4" Grid.Column="0" Grid.ColumnSpan="2">
        <oxy:PlotView Background="White" Model="{Binding GraphicModel.Model}"  >

        </oxy:PlotView>
    </Border>

    <TextBlock Text="{Binding CurrentTrackerValue}" Grid.Column="0" Grid.Row="1"/>

I know, what PlotView.Model have event TrackerChange . 我知道,什么PlotView.Model具有事件TrackerChange How using this event? 如何使用此事件? PS: I use pattern MVVM, so i want using command instead event. PS:我使用模式MVVM,所以我想使用命令代替事件。 Thank! 谢谢!

If I understand your requirement correctly, you want to update a TextBlock each time the Tracker is updated. 如果我正确理解您的要求,则每次跟踪器更新时都希望更新一个TextBlock。 I believe you were right on track with the TrackerChanged event. 我相信您在TrackerChanged事件上正步入正轨。 You could do the following in your ViewModel, where you are creating the PlotModel instance, which would be bound to the OxyPlot Graph 您可以在要创建PlotModel实例的ViewModel中执行以下操作,该实例将绑定到OxyPlot Graph

PlotModelName.TrackerChanged += (sender, eventArgs)=>
{
  CurrentTrackerValue = eventArgs.HitResult != null ? eventArgs.HitResult.Text : CurrentTrackerValue;
  NotifyPropertyChanged(nameof(CurrentTrackerValue));
};

Where CurrentTrackerValue is defined as 其中CurrentTrackerValue定义为

public string CurrentTrackerValue { get; set; }

This would ensure the CurrentTrackerValue property is updated each time Tracker is changed via the TrackerChangedEvent 这样可以确保每次通过TrackerChangedEvent更改跟踪器时,都会更新CurrentTrackerValue属性。

guys thanks it works steps bellow :) 伙计们感谢它在下面的步骤工作:)

.NET 4.7.2 .NET 4.7.2

  1. I added INotifyPropertyChanged and 我添加了INotifyPropertyChanged
public string CurrentTrackerValueX { get; set; }
      public string CurrentTrackerValueY { get; set; }
  1. then generate 然后生成
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) 
    { 
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
    }
  1. then i follow your idea 然后我跟随你的想法
pm.TrackerChanged += (sender, eventArgs) =>
    {
        string CurrentTrackerValue = "";
        CurrentTrackerValue = eventArgs.HitResult != null ? eventArgs.HitResult.Text : CurrentTrackerValue;
        if (!String.IsNullOrEmpty(CurrentTrackerValue))
        {
            var x = Regex.Matches(CurrentTrackerValue, "([0-9]*,[0-9]*)");
            CurrentTrackerValueX = x[0].Value;
            CurrentTrackerValueY = x[1].Value;
            OnPropertyChanged(nameof(CurrentTrackerValueX));
            OnPropertyChanged(nameof(CurrentTrackerValueY));
        }
    };

If loop is needed cause of null value in textbox after leaving Tracker, he still want gives null to textbox. If loop需要If loop则在离开Tracker后在文本框中输入null值的原因,他仍然希望为textbox提供null。

All done and works with this type of string :) 完成并使用这种类型的字符串:)

editcurrentLineSeries[i].TrackerFormatString = "{0}\n{1}: {2:0.00}\n{3}: {4:0.00}";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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