[英]How can I evaluate the x-axis value, knowing the position of the mouse, in the model?
我正在使用 MVVM 模式,我通過鼠標行為的附件在我的Plot
中的 Plot 上安裝了鼠標 position。
我正在使用DateTimeAxis
作為 x 軸,我想從我的 x 位置獲取 x 軸值,但我不知道如何繼續。
如果我不使用 MVVM 模式,實現我想要的一個好方法是:
XAML
<oxy:Plot x:Name="TopPlot" MouseMove="TopPlot_MouseMove" >
<oxy:Plot.Axes>
<oxy:DateTimeAxis x:Name="DateAxis" Position="Bottom" />
<oxy:LinearAxis x:Name="ValueAxis" Title="Value" Position="Left"/>
</oxy:Plot.Axes>
</oxy:Plot>
后面的代碼:
private void TopPlot_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
var x_axis = this.TopPlot.ActualModel.DefaultXAxis;
var y_axis = this.TopPlot.ActualModel.DefaultYAxis;
var point = OxyPlot.Axes.Axis.InverseTransform(new ScreenPoint(e.GetPosition(TopPlot).X,
e.GetPosition(TopPlot).Y), x_axis, y_axis);
}
我嘗試使用 Plot 的屬性 Model 進行OneWayToSource
綁定(以便我可以在非 MVVM 模型中做一些鏈接),但我在我的屬性中收到的值是null
。
XAML
<oxy:Plot Model="{Binding Path=Plot_Model, Mode=OneWayToSource}" >
<oxy:Plot.Series>
<oxy:LineSeries ItemsSource="{Binding m_Series,
UpdateSourceTrigger=PropertyChanged}"/>
</oxy:Plot.Series>
<oxy:Plot.Axes>
<oxy:DateTimeAxis Position="Bottom"/>
<oxy:LinearAxis Position="Left" Title="Value"/>
</oxy:Plot.Axes>
<i:Interaction.Behaviors>
<mouseMoveMvvm:MouseBehaviour MouseX="{Binding PlotX, Mode=OneWayToSource}"
MouseY="{Binding PlotY, Mode=OneWayToSource}"/>
</i:Interaction.Behaviors>
</oxy:Plot>
我的 Model 中的代碼:
private double _plotX;
public double PlotX
{
get { return _plotX; }
set
{
if (value.Equals(_plotX)) return;
_plotX = value;
NotifyPropertyChanged();
NotifyPropertyChanged(nameof(PositionText));
}
}
private double _plotY;
public double PlotY
{
get { return _plotY; }
set
{
if (value.Equals(_plotY)) return;
_plotY = value;
NotifyPropertyChanged();
NotifyPropertyChanged(nameof(PositionText));
}
}
private PlotModel m_model;
public PlotModel Plot_Model
{
set
{
m_model = value;
}
}
public string PositionText
{
get
{
//var x_axis = m_model.DefaultXAxis;
//var y_axis = m_model.DefaultYAxis;
//DataPoint point = Axis.InverseTransform(new ScreenPoint(_plotX, _plotY), x_axis,
// y_axis);
//return string.Format("Pos. X: {0}, /n Pos. Y: {1} ", point.X, point.Y);
return string.Format("Pos. X: {0}, /n Pos. Y: {1} ", _plotX, _plotY);
}
}
有人對如何進行有任何建議嗎?
我不知道您從哪里得到MouseBehavior
,但它可能與Plot
不兼容。 我認為最簡單的方法是創建符合您要求的自己的行為,因為您已經有了一個有效的代碼隱藏解決方案。 如果你把它翻譯成一種行為,它會是這樣的。
public class PlotMousePositionBehaviour : Behavior<Plot>
{
public static readonly DependencyProperty YProperty = DependencyProperty.Register(
nameof(Y), typeof(double), typeof(PlotMousePositionBehaviour));
public static readonly DependencyProperty XProperty = DependencyProperty.Register(
nameof(X), typeof(double), typeof(PlotMousePositionBehaviour));
public double Y
{
get => (double)GetValue(YProperty);
set => SetValue(YProperty, value);
}
public double X
{
get => (double)GetValue(XProperty);
set => SetValue(XProperty, value);
}
protected override void OnAttached()
{
AssociatedObject.MouseMove += OnMouseMove;
}
protected override void OnDetaching()
{
AssociatedObject.MouseMove -= OnMouseMove;
}
private void OnMouseMove(object sender, MouseEventArgs e)
{
var xAxis = AssociatedObject.ActualModel.DefaultXAxis;
var yAxis = AssociatedObject.ActualModel.DefaultYAxis;
var screenPoint = new ScreenPoint(e.GetPosition(AssociatedObject).X, e.GetPosition(AssociatedObject).Y);
var point = OxyPlot.Axes.Axis.InverseTransform(screenPoint, xAxis, yAxis);
X = point.X;
Y = point.Y;
}
}
您可以進一步擴展它以添加自定義轉換器的屬性,而不是硬連線點轉換。 通過這種行為,您可以將X
和Y
坐標綁定到您的視圖 model。
<oxy:Plot x:Name="TopPlot">
<b:Interaction.Behaviors>
<local:PlotMousePositionBehaviour X="{Binding PlotX}"
Y="{Binding PlotY}"/>
</b:Interaction.Behaviors>
<oxy:Plot.Axes>
<oxy:DateTimeAxis x:Name="DateAxis" Position="Bottom" />
<oxy:LinearAxis x:Name="ValueAxis" Title="Value" Position="Left"/>
</oxy:Plot.Axes>
</oxy:Plot>
關於您的PositionText
,這應該是一個觀點問題。 您可以直接在視圖中綁定X
和Y
並格式化文本。 這是一個示例,為行為分配一個x:Name
。
<b:Interaction.Behaviors>
<local:PlotMousePositionBehaviour x:Name="PlotMousePositionBehaviour"/>
</b:Interaction.Behaviors>
通過TexBlock
中的ElementName
引用屬性(或者使用StringFormat
)。
<TextBlock>
<Run Text="{Binding X, ElementName=PlotMousePositionBehaviour, StringFormat={}Pos. X: {0}}"/>
<LineBreak/>
<Run Text="{Binding Y, ElementName=PlotMousePositionBehaviour, StringFormat={}Pos. Y: {0}}"/>
</TextBlock>
這樣,您甚至不需要對視圖 model 進行間接處理,如果這些值未在此處使用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.