问题描述
我有一个嵌套的WPF视图,在这里我通过依赖属性将对象传递到内层。
我的外部XAML层将数据传递到RetentionChartControl.ChartParams
:
<local:RetentionChartControl ChartParams="{Binding PeakRow.Chartparams[0],RelativeSource={RelativeSource AncestorType={x:Type local:PeakrowChartGroupControl}}}"></local:RetentionChartControl>
<UserControl x:Class="DryLab.Peakmovement.RetentionChartControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:DryLab.Peakmovement"
xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
mc:Ignorable="d"
x:Name="uc"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<lvc:CartesianChart x:Name="cc" Series="{Binding ChartParams.Seriescollection,RelativeSource={RelativeSource AncestorType={x:Type local:RetentionChartControl}}}" >
<lvc:CartesianChart.DataTooltip>
<lvc:DefaultTooltip SelectionMode="OnlySender" />
</lvc:CartesianChart.DataTooltip>
<lvc:CartesianChart.AxisY>
<lvc:Axis Foreground="Black" Title="{Binding ChartParams.TitleAxY,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type local:RetentionChartControl}} }" LabelFormatter="{Binding ChartParams.Formatter,RelativeSource={RelativeSource AncestorType={x:Type local:RetentionChartControl}}}">
</lvc:Axis>
</lvc:CartesianChart.AxisY>
<lvc:CartesianChart.AxisX>
<lvc:Axis Foreground="Black" Title="{Binding ChartParams.TitleAxX,RelativeSource={RelativeSource AncestorType={x:Type local:RetentionChartControl}}}" Labels="{Binding ChartParams.Labels,RelativeSource={RelativeSource AncestorType={x:Type local:RetentionChartControl}} }"></lvc:Axis>
</lvc:CartesianChart.AxisX>
</lvc:CartesianChart>
</Grid>
</UserControl>
现在<lvc:Cartesian Chart>
(从LiveCharts https://lvcharts.net/App/examples/v1/wpf/Basic%20Line%20Chart)的绑定正在工作,ChartParams变量已完全评估,我可以访问Seriescollection。
但是,如果我查看更深层的嵌套<lvc:Axis>
(通过LiveVisualTree),则不会评估Title
属性。
我错过了什么?为什么我不能从<lvc:Axis>
内绑定到ChartParams?
我们将竭诚为您服务,如有需要,我们将发布更多代码。
编辑:
我应该补充一点,当应用程序运行时,当我在Xaml中触摸Axis.Title
时,它将重新评估,然后按预期显示结果
解决方法
问题是,Axis
元素在解析绑定后没有添加到可视化树中。稍后添加作为图表基础Canvas
的子元素的所有可见元素(在引发图表控件的FrameworkElement.Loaded
事件之后很长时间)。因此,绑定数据的绑定例如Axis
或Separator
等使用RelativeSource
指向其可视父级的对象将指向无处(除非您在将相关目标元素添加到Canvas
之后设置绑定(需要C#而不是XAML)来观察CartesianChart.Content.LayoutUpdated
事件,但我反对这样做)。
推荐的解决方案是改为绑定到DataContext
。
您可以将UserControl
本身设置为自己的DataContext
:
PeakrowChartGroupControl.xaml.cs
partial class PeakrowChartGroupControl : UserControl
{
public PeakrowChartGroupControl()
{
InitializeComponent();
this.DataContext = this;
}
}
PeakrowChartGroupControl.xaml
<UserControl x:Class="DryLab.Peakmovement.RetentionChartControl">
<Grid>
<lvc:CartesianChart x:Name="cc" Series="{Binding ChartParams.Seriescollection}">
<lvc:CartesianChart.AxisY>
<lvc:Axis Title="{Binding ChartParams.TitleAxY}"
LabelFormatter="{Binding ChartParams.Formatter}" />
</lvc:CartesianChart.AxisY>
<lvc:CartesianChart.AxisX>
<lvc:Axis Title="{Binding ChartParams.TitleAxX}"
Labels="{Binding ChartParams.Labels}" />
</lvc:CartesianChart.AxisX>
</lvc:CartesianChart>
</Grid>
</UserControl>
或者例如,如果UserControl.DataContext
已经设置为其他设置,则可以将图表DataContext
显式设置为ChartParams
的{{1}}属性:
PeakrowChartGroupControl.xaml
RetentionChartControl