Microsoft の WPF ツールキットの Chart Control を使用して、独自のチャート コントロールを作成しました。私はそれについてここにブログを書きました。My Chart コントロールは、チャート内の yaxes を互いの上に積み重ねます。記事で読むことができるように、これはすべて非常にうまく機能します。ここで、グラフのデータと軸を制御するビューモデルを作成したいと考えています。これまでのところ、軸をグラフに追加してグラフに表示することができました。しかし、1 つの DependentAxis プロパティと 1 つの InDependentAxis プロパティがあるため、lineseries を追加しようとすると問題が発生します。適切な xAxis および yAxis コントロールを割り当てる方法がわかりません。
以下に、LineSeriesViewModel の一部を示します。ネストされた XAxisViewModel および YAxisViewModel プロパティがあります。
public class LineSeriesViewModel : ViewModelBase, IChartComponent
{
XAxisViewModel _xAxis;
public XAxisViewModel XAxis
{
get { return _xAxis; }
set
{
_xAxis = value;
RaisePropertyChanged(() => XAxis);
}
}
//The YAxis Property look the same
}
すべてのビューモデルには、独自のデータ テンプレートがあります。xaml コードは次のようになります。
<UserControl.Resources>
<DataTemplate x:Key="xAxisTemplate" DataType="{x:Type l:YAxisViewModel}">
<chart:LinearAxis x:Name="yAxis" Orientation="Y" Location="Left" Minimum="0" Maximum="10" IsHitTestVisible="False" Width="50" />
</DataTemplate>
<DataTemplate x:Key="yAxisTemplate" DataType="{x:Type l:XAxisViewModel}">
<chart:LinearAxis x:Name="xAxis" Orientation="X" Location="Bottom" Minimum="0" Maximum="100" IsHitTestVisible="False" Height="50" />
</DataTemplate>
<DataTemplate DataType="{x:Type l:LineSeriesViewModel}">
<!--Binding doesn't work on the Dependent and IndependentAxis! -->
<!--YAxis XAxis and Series are properties of the LineSeriesViewModel -->
<l:FastLineSeries DependentAxis="{Binding Path=YAxis}"
IndependentAxis="{Binding Path=XAxis}"
ItemsSource="{Binding Path=Series}"/>
</DataTemplate>
<Style TargetType="ItemsControl">
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<!--My stacked chart control -->
<l:StackedPanel x:Name="stackedPanel" Width="Auto" Height="Auto" Background="LightBlue">
</l:StackedPanel>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ClipToBounds="True">
<!-- View is an ObservableCollection of all axes and series-->
<ItemsControl x:Name="chartItems" ItemsSource="{Binding Path=View}" Focusable="False">
</ItemsControl>
</Grid>
このコードは非常にうまく機能します。軸を追加すると、軸が描画されます。しかし、lineseries コントロールの DependentAxis と InDependentAxis は null のままであるため、シリーズは描画されません。ネストされたビューモデルをコントロールのプロパティにバインドするにはどうすればよいですか?