简体   繁体   English

在 WPF 中使用实时图表将数据绑定到 CartesianChart 而无需设置数据上下文

[英]Bind data to CartesianChart without setting datacontext using Live Chart in WPF

When I set up datacontext like this then everything works fine.当我像这样设置数据上下文时,一切正常。 My backend code我的后端代码

public partial class ucMyGraph : UserControl
{
    public ucMyGraph()
    {
        InitializeComponent();
        DataContext = this;
    }
    public ChartValues<decimal> ChartValues
    {
        get { return (ChartValues<decimal>)GetValue(ChartValuesProperty); }
        set { SetValue(ChartValuesProperty, value); }
    }
    public static readonly DependencyProperty ChartValuesProperty =
        DependencyProperty.Register("ChartValues", typeof(ChartValues<decimal>), typeof(ucWatchlistGraph),
            new PropertyMetadata(new ChartValues<decimal> { 1816.59m, 1818.85m, 1820.71m, 1821.03m, 1824.32m, 1825.62m, 1825.58m, 1826.71m, 1825.62m, 1824.76m, 1825.05m,
        1823.71m, 1824.66m, 1825.54m, 1824.67m, 1826.1m, 1820.66m, 1822.35m, 1822.06m, 1822.22m }));
}

And here is the xmal这是xmal

 <lvc:CartesianChart Height="60" Width="120"  Margin="0" Padding="0" x:Name="cartesianChart" 
                            AnimationsSpeed="0:0:0.5" Hoverable="False" 
                            DataTooltip="{x:Null}">
        <lvc:CartesianChart.Series>
            <lvc:LineSeries Values="{Binding ChartValues}" 
                            PointGeometry="{x:Null}" 
                            LineSmoothness="0.1"
                            StrokeThickness="1"                                 
                            Stroke="#43A047">
            </lvc:LineSeries>
        </lvc:CartesianChart.Series>
    </lvc:CartesianChart>

When I erase this line DataContext = this form the constractor and try to bind ChartValues using relative resource like this ( Binding RelativeSource={RelativeSource AncestorType={x:Type local:ucMyGraph}}, Path=ChartValues ) it doesn't work.当我擦除此行DataContext = this形成构造器并尝试使用这样的相对资源绑定 ChartValues ( Binding RelativeSource={RelativeSource AncestorType={x:Type local:ucMyGraph}}, Path=ChartValues ) 它不起作用。 I'm wondering why it is important to set datacontext for showing the live chart graph.我想知道为什么设置数据上下文以显示实时图表很重要。 I'm new in this topic so I need your help.我是这个主题的新手,所以我需要你的帮助。 I want to show the graph without initialize datacontext.我想在不初始化数据上下文的情况下显示图形。 How do I achieve this?我如何实现这一目标?

DataContext is use for store data to display to UI(User Interface). DataContext 用于存储数据以显示到 UI(用户界面)。 If you set Values="{Binding ChartValues}", it will understand that you are binding ChartValues of DataContext.如果您设置 Values="{Binding ChartValues}",它会理解您正在绑定 DataContext 的 ChartValues。 In the constructor ucMyGraph() you assigned DataContext = this (ucMyGraph), which mean the "Values" will display the value of property ChartValues of this(ucMyGraph).在构造函数 ucMyGraph() 中,您指定了 DataContext = this (ucMyGraph),这意味着“Values”将显示 this(ucMyGraph) 的属性 ChartValues 的值。 If the DataContext you assigned doesn't have the property ChartValues, the binding will not work.如果您分配的 DataContext 没有属性 ChartValues,绑定将不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM