C# OxyPlot中的多个线型绑定
是否可以将绘图绑定到线系列集合而不是OxyPlot中的单个线系列?(而不是通过模型) 我在找这样的东西:C# OxyPlot中的多个线型绑定,c#,wpf,plot,oxyplot,C#,Wpf,Plot,Oxyplot,是否可以将绘图绑定到线系列集合而不是OxyPlot中的单个线系列?(而不是通过模型) 我在找这样的东西: <oxy:Plot> <oxy:Plot.Series> <oxy:LineSeries ItemsSource="{Binding myCollectionOfLineSeries}" /> </oxy:Plot.Series> </oxy:Plot&
<oxy:Plot>
<oxy:Plot.Series>
<oxy:LineSeries ItemsSource="{Binding myCollectionOfLineSeries}" />
</oxy:Plot.Series>
</oxy:Plot>
其中,myCollectionOfLineSeries是:
private ObservableCollection<LineSeries> _myCollectionOfLineSeries ;
public ObservableCollection<LineSeries> myCollectionOfLineSeries
{
get
{
return _myCollectionOfLineSeries ;
}
set
{
_myCollectionOfLineSeries = value;
OnPropertyChanged("myCollectionOfLineSeries ");
}
}
private observedcollection\u myCollectionOfLineSeries;
公共可观测集合线性系列集合
{
得到
{
return _myCollectionOfLineSeries;
}
设置
{
_myCollectionOfLineSeries=值;
OnPropertyChanged(“myCollectionOfLineSeries”);
}
}
我希望答案是:a)“不,这是不可能的”或b)“是的,把XYZ放在IJK之前”
感谢阅读。是的,请查看他们的,您需要绑定到数据点的集合
public ObservableCollection<DataPoint> MyCollection { ... }
不过,您可以绑定到Model
属性,该属性是一种PlotModel
类型,具有带有getter和setter的Series
集合属性。有关更多详细信息,请查看
<oxy:Plot Model="{Binding MyModel}" ...
可能有点晚了,但最近我遇到了同样的问题:我需要动态绘制多个系列(基于用户选择的货币的多个收益率曲线),但我不想使用PlotModel
直接绑定plot
,就像其他属性一样(例如Title
)需要在我的视图模型中设置为代码,而不是XAML标记
因此,我将PlotModel
定义为资源,将其绑定到绘图。并在加载视图时查找PlotModel。通过这种方法,我可以通过XAML标记定义可视内容(例如标题、轴、图例等),同时将生成序列的逻辑放入视图模型代码中
不确定这是不是一个好方法,但它解决了我的问题
1) XAML-定义资源
<UserControl.Resources>
<oxyPlot:PlotModel
x:Key="TestPlotModel"
Title="XXX Curves (Preview)"
Subtitle="Scroll mousewheel to zoom; Right-drag to pan"
LegendPlacement="Outside"
LegendBorder="{x:Static Member=oxyPlot:OxyColors.Black}"
>
<oxyPlot:PlotModel.Axes>
<axes:LinearAxis
Title="Rate (%)"
Position="Left"
StartPosition="0"
StringFormat="#.00000"
MajorGridlineStyle="Solid"
MajorGridlineColor="{x:Static Member=oxyPlot:OxyColors.LightGray}"
MinorGridlineStyle="Dot"
MinorGridlineColor="{x:Static Member=oxyPlot:OxyColors.LightGray}"
>
</axes:LinearAxis>
<axes:LinearAxis
Title="Maturity (Days)"
Position="Bottom"
StartPosition="0"
MajorGridlineStyle="Solid"
MajorGridlineColor="{x:Static Member=oxyPlot:OxyColors.LightGray}"
>
</axes:LinearAxis>
</oxyPlot:PlotModel.Axes>
</oxyPlot:PlotModel>
</UserControl.Resources>
3) 视图模型-从视图中获取模型,但不绑定
protected override void OnViewLoaded(object view)
{
base.OnViewLoaded(view);
this._model = (PlotModel)((XXXView)view).FindResource("TestPlotModel");
}
4) 视图模型-生成多个系列
_model.Series.Clear();
foreach (var currency in distinctCurrencies)
{
IEnumerable<DataPoint> dataPoints = ...;
LineSeries lineSeries = new LineSeries()
{
Title = String.Format("*{0}", currency),
ItemsSource = dataPoints
};
_model.Series.Add(lineSeries);
}
\u model.Series.Clear();
foreach(以不同货币表示的var货币)
{
IEnumerable数据点=。。。;
LineSeries LineSeries=新的LineSeries()
{
Title=String.Format(“*{0}”,货币),
ItemsSource=数据点
};
_model.Series.Add(lineSeries);
}
希望有帮助 我不知道我是否理解你!从我的观点来看,你在回答中的建议与绑定单个系列相同。我想知道是否可以绑定到LineSeries集合,或者在本例中绑定到DataPoint集合。谢谢“属性”系列“没有可访问的setter”,它说,我已经尝试了几种绑定“系列”的方法,但到目前为止没有成功。也不起作用。objo(OxyPlot的创建者):“不,这是不可能的。要显示的每一行都需要一个LineSeries。创建一个PlotModel并绑定Model属性,或者使用code behind更动态地操作Series集合。我确信也有其他方法仅在XAML中执行此操作(可能使用附加属性??)”。谢谢,我终于按照您的建议使用了该模型。2017年3月19日:模型
不再是
的财产,但
这不是解决方案。应将PlotModel移动到ViewModel中,并在ViewModel中添加多个系列。PlotModel必须从ViewModel绑定。这是不可能的:这个._model=(PlotModel)((XXXView)视图).FindResource(“TestPlotModel”);
<oxy:Plot Grid.Row="1" Model="{Binding Source={StaticResource TestPlotModel}}">
</oxy:Plot>
protected override void OnViewLoaded(object view)
{
base.OnViewLoaded(view);
this._model = (PlotModel)((XXXView)view).FindResource("TestPlotModel");
}
_model.Series.Clear();
foreach (var currency in distinctCurrencies)
{
IEnumerable<DataPoint> dataPoints = ...;
LineSeries lineSeries = new LineSeries()
{
Title = String.Format("*{0}", currency),
ItemsSource = dataPoints
};
_model.Series.Add(lineSeries);
}