猿问

用户控件依赖属性未更新

我为 livechart 创建了一个用户控件,并尝试围绕它创建依赖属性。但是依赖属性没有得到更新。我用谷歌搜索并在 StackOverflow 中找到了答案。所以我一直在四处寻找。我按照答案中提到的步骤操作,但仍然无法更新图表。代码:-


<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);

    }


更改的事件被命中并且值被更新。但仍然没有反映在图表中。所以有点困惑。


问题是当我尝试使用WPF 工具 Snoop并单击线系列时,我可以看到图表立即更新,


点击前:-

点击后:-

http://img.mukewang.com/617541c40001230114260751.jpg

精慕HU
浏览 249回答 1
1回答

慕的地10843

发布答案,以便它可以帮助像我这样的人。我能够找到这个博客帮助我的答案。正如他们所提到的,我需要在 Xaml 中添加数据上下文。LiveChart:CartesianChart Grid.Row="1" Background="White" DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:LiveChartControl}}">&nbsp; &nbsp; &nbsp; &nbsp; <LiveChart:CartesianChart.AxisX>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <LiveChart:Axis Title="{Binding Path=XAxisTitle, Mode=TwoWay}" Labels="{Binding Path=XAxisValues, Mode=TwoWay}" MinValue="0"/>&nbsp; &nbsp; &nbsp; &nbsp; </LiveChart:CartesianChart.AxisX>&nbsp; &nbsp; &nbsp; &nbsp; <LiveChart:CartesianChart.AxisY>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <LiveChart:Axis Title="{Binding Path=YAxisTitle, Mode=TwoWay}" LabelFormatter="{Binding Path=Formatter, Mode=TwoWay}" MinValue="0"></LiveChart:Axis>&nbsp; &nbsp; &nbsp; &nbsp; </LiveChart:CartesianChart.AxisY>&nbsp; &nbsp; &nbsp; &nbsp; <LiveChart:CartesianChart.Series>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <LiveChart:LineSeries Values="{Binding Path=ChartPointsInChartValues, Mode=TwoWay}"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <LiveChart:VerticalLineSeries Values="{Binding Path=EnergyLineChartValues , Mode=TwoWay}"/>&nbsp; &nbsp; &nbsp; &nbsp; </LiveChart:CartesianChart.Series>&nbsp; &nbsp; </LiveChart:CartesianChart>这现在开始起作用了。
随时随地看视频慕课网APP
我要回答