用户控件的依赖项属性未更新

时间:2018-07-24 10:32:57

标签: c# wpf xaml livecharts

我为livechart创建了一个用户控件,并尝试围绕它创建依赖项属性。但是依赖项属性没有更新。我用Google搜索并找到了answer in 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);
    }

    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";
    }

已更改的事件被命中,并且值已更新。但仍未反映在图表中。有点困惑。

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

点击之前:- Before click

点击后:- After click

1 个答案:

答案 0 :(得分:0)

发布答案,以便对像我这样的人有所帮助。 我能够找到答案this blog对我有帮助。正如他们提到的,我需要在Xaml中添加datacontext。

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>

现在开始工作。