【问题标题】:User control Dependency property is not getting updated用户控件依赖属性未更新
【发布时间】:2018-07-26 13:01:37
【问题描述】:

我为实时图表创建了一个用户控件,并尝试围绕它创建依赖属性。但是依赖属性没有得到更新。我用谷歌搜索并找到了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 并单击线系列时,我可以看到图表立即更新,

点击前:-

点击后:-

【问题讨论】:

  • 在 UpdateChart 中,您正在更新一些(内部?)属性(如 SpectrumChartSeries 等)。这些属性是否会通知更改?注意,当然没有必要在它们的绑定上设置UpdateSourceTrigger=PropertyChanged,因为绑定不是双向的。
  • 它写在 xaml.cs 文件中,所以我没有写任何通知更改事件。我是不是该?因为它是背后的代码。它必须立即更新。
  • {Binding Path=SpectrumChartSeries, ...} 这样的绑定仅在源属性触发更改通知时更新其目标属性。因此 SpectrumChartSeries 必须是依赖属性,或者其所属类必须实现 INotifyPropertyChanged 并在 SpectrumChartSeries 设置器中触发 PropertyChanged 事件。
  • @Clemens 我尝试添加 INotifyPropertyChanged。还是不行。 propertychanged 没有被击中,谷歌搜索并发现设置数据上下文。也这样做了。还是行不通。请任何其他方法

标签: c# wpf xaml livecharts


【解决方案1】:

发布答案,以便它可以帮助像我这样的人。 我能够找到答案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>

现在开始工作了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-06
    • 1970-01-01
    相关资源
    最近更新 更多