用户控件依赖项 属性 未更新
User control Dependency property is not getting updated
我已经为 livechart 创建了一个用户控件,并试图围绕它创建依赖属性。但是依赖项 属性 没有得到更新。我用谷歌搜索并找到了 answer in Whosebug。所以我一直在四处寻找。我按照答案中提到的步骤进行操作,但我仍然无法更新图表。
代码:-
<LiveChart:CartesianChart Grid.Row="1" Background="White">
<LiveChart:CartesianChart.AxisX>
<LiveChart:Axis Title="{Binding Path=XAxisTitle, ElementName=chartControl, UpdateSourceTrigger=PropertyChanged}" Labels="{Binding Path=XAxisValues, ElementName=chartControl, UpdateSourceTrigger=PropertyChanged}"/>
</LiveChart:CartesianChart.AxisX>
<LiveChart:CartesianChart.AxisY>
<LiveChart:Axis Title="{Binding Path=YAxisTitle, ElementName=chartControl, UpdateSourceTrigger=PropertyChanged}" LabelFormatter="{Binding Path=Formatter, ElementName=chartControl, UpdateSourceTrigger=PropertyChanged}" MinValue="0"></LiveChart:Axis>
</LiveChart:CartesianChart.AxisY>
<LiveChart:CartesianChart.Series>
<LiveChart:LineSeries Values="{Binding Path=SpectrumChartSeries, ElementName=chartControl, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"></LiveChart:LineSeries>
</LiveChart:CartesianChart.Series>
</LiveChart:CartesianChart>
我已将用户控件命名为 chartControl 并使用了 ElementName。
我的后端:-
public List<Point> ChartPoints
{
get { return (List<Point>)GetValue(ChartPointsProperty); }
set { SetValue(ChartPointsProperty, value); }
}
public static readonly DependencyProperty ChartPointsProperty = DependencyProperty.Register("ChartPoints", typeof(List<Point>), typeof(chartcontrol), new FrameworkPropertyMetadata(new List<Point>(), onChartrPointChange));
private static void onChartrPointChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var control = d as chartcontrol;
var points = e.NewValue as List<Point>;
control.UpdateChart(points);
}
private void UpdateChart(List<Point> chartPoints)
{
SpectrumChartSeries = new ChartValues<double>();// { 4, 6, 5, 2, 4, 8, 9, 10, 20, 11, 15 };
SpectrumChartSeries.AddRange(chartPoints.Select(x => x.Y));
XAxisValues = chartPoints.Select(x => x.X.ToString()).ToList();//new List<string>() { "1", "2", "3", "4", "5" };
Formatter = value => (value).ToString("N", System.Globalization.CultureInfo.CurrentCulture);
XAxisTitle = " test title";
YAxisTitle = "y title";
}
changed 事件正在命中并且值已更新。但仍然没有反映在图表中。有点困惑。
问题是当我尝试使用 WPF tool Snoop 并单击线系列时,我可以看到图表立即得到更新,
点击前:-
点击后:-
发布答案以帮助像我这样的人。
我能够找到 this blog 帮助我的答案。正如他们所提到的,我需要在 Xaml.
中添加数据上下文
LiveChart:CartesianChart Grid.Row="1" Background="White" DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:LiveChartControl}}">
<LiveChart:CartesianChart.AxisX>
<LiveChart:Axis Title="{Binding Path=XAxisTitle, Mode=TwoWay}" Labels="{Binding Path=XAxisValues, Mode=TwoWay}" MinValue="0"/>
</LiveChart:CartesianChart.AxisX>
<LiveChart:CartesianChart.AxisY>
<LiveChart:Axis Title="{Binding Path=YAxisTitle, Mode=TwoWay}" LabelFormatter="{Binding Path=Formatter, Mode=TwoWay}" MinValue="0"></LiveChart:Axis>
</LiveChart:CartesianChart.AxisY>
<LiveChart:CartesianChart.Series>
<LiveChart:LineSeries Values="{Binding Path=ChartPointsInChartValues, Mode=TwoWay}"/>
<LiveChart:VerticalLineSeries Values="{Binding Path=EnergyLineChartValues , Mode=TwoWay}"/>
</LiveChart:CartesianChart.Series>
</LiveChart:CartesianChart>
这现在开始起作用了。
我已经为 livechart 创建了一个用户控件,并试图围绕它创建依赖属性。但是依赖项 属性 没有得到更新。我用谷歌搜索并找到了 answer in Whosebug。所以我一直在四处寻找。我按照答案中提到的步骤进行操作,但我仍然无法更新图表。 代码:-
<LiveChart:CartesianChart Grid.Row="1" Background="White">
<LiveChart:CartesianChart.AxisX>
<LiveChart:Axis Title="{Binding Path=XAxisTitle, ElementName=chartControl, UpdateSourceTrigger=PropertyChanged}" Labels="{Binding Path=XAxisValues, ElementName=chartControl, UpdateSourceTrigger=PropertyChanged}"/>
</LiveChart:CartesianChart.AxisX>
<LiveChart:CartesianChart.AxisY>
<LiveChart:Axis Title="{Binding Path=YAxisTitle, ElementName=chartControl, UpdateSourceTrigger=PropertyChanged}" LabelFormatter="{Binding Path=Formatter, ElementName=chartControl, UpdateSourceTrigger=PropertyChanged}" MinValue="0"></LiveChart:Axis>
</LiveChart:CartesianChart.AxisY>
<LiveChart:CartesianChart.Series>
<LiveChart:LineSeries Values="{Binding Path=SpectrumChartSeries, ElementName=chartControl, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"></LiveChart:LineSeries>
</LiveChart:CartesianChart.Series>
</LiveChart:CartesianChart>
我已将用户控件命名为 chartControl 并使用了 ElementName。 我的后端:-
public List<Point> ChartPoints
{
get { return (List<Point>)GetValue(ChartPointsProperty); }
set { SetValue(ChartPointsProperty, value); }
}
public static readonly DependencyProperty ChartPointsProperty = DependencyProperty.Register("ChartPoints", typeof(List<Point>), typeof(chartcontrol), new FrameworkPropertyMetadata(new List<Point>(), onChartrPointChange));
private static void onChartrPointChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var control = d as chartcontrol;
var points = e.NewValue as List<Point>;
control.UpdateChart(points);
}
private void UpdateChart(List<Point> chartPoints)
{
SpectrumChartSeries = new ChartValues<double>();// { 4, 6, 5, 2, 4, 8, 9, 10, 20, 11, 15 };
SpectrumChartSeries.AddRange(chartPoints.Select(x => x.Y));
XAxisValues = chartPoints.Select(x => x.X.ToString()).ToList();//new List<string>() { "1", "2", "3", "4", "5" };
Formatter = value => (value).ToString("N", System.Globalization.CultureInfo.CurrentCulture);
XAxisTitle = " test title";
YAxisTitle = "y title";
}
changed 事件正在命中并且值已更新。但仍然没有反映在图表中。有点困惑。
问题是当我尝试使用 WPF tool Snoop 并单击线系列时,我可以看到图表立即得到更新,
点击前:-
点击后:-
发布答案以帮助像我这样的人。 我能够找到 this blog 帮助我的答案。正如他们所提到的,我需要在 Xaml.
中添加数据上下文LiveChart:CartesianChart Grid.Row="1" Background="White" DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:LiveChartControl}}">
<LiveChart:CartesianChart.AxisX>
<LiveChart:Axis Title="{Binding Path=XAxisTitle, Mode=TwoWay}" Labels="{Binding Path=XAxisValues, Mode=TwoWay}" MinValue="0"/>
</LiveChart:CartesianChart.AxisX>
<LiveChart:CartesianChart.AxisY>
<LiveChart:Axis Title="{Binding Path=YAxisTitle, Mode=TwoWay}" LabelFormatter="{Binding Path=Formatter, Mode=TwoWay}" MinValue="0"></LiveChart:Axis>
</LiveChart:CartesianChart.AxisY>
<LiveChart:CartesianChart.Series>
<LiveChart:LineSeries Values="{Binding Path=ChartPointsInChartValues, Mode=TwoWay}"/>
<LiveChart:VerticalLineSeries Values="{Binding Path=EnergyLineChartValues , Mode=TwoWay}"/>
</LiveChart:CartesianChart.Series>
</LiveChart:CartesianChart>
这现在开始起作用了。