依赖属性到图表

时间:2012-08-02 09:25:15

标签: c# wpf xaml dependency-properties

我有一个依赖属性,我想在图表上显示。

namespace ViewModels
{
  public partial class MyVM: DependencyObject
  {
    public Double TotalPowerSoFar
        {
            get { return (Double)GetValue(TotalPowerSoFarProperty); }
            set { SetValue(TotalPowerSoFarProperty, value); }
        }

        // Using a DependencyProperty as the backing store for GroupReadingsBy.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TotalPowerSoFarProperty =
            DependencyProperty.Register("TotalPowerSoFar", typeof(Double), typeof(EstimateVM), new UIPropertyMetadata(0.0));


    TotalPowerSoFar=5.0;
   }
}

我以为我会做那样的事情:

<UserControl x:Class="Workspace"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:DV="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit"
             xmlns:DVC="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
             mc:Ignorable="d"
             d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<DVC:Chart Name="mcChart"   Foreground="DarkBlue" Title="Area Chart" LegendTitle="Month Rating" >
                    <DVC:Chart.Series>
                        <DVC:ColumnSeries DependentValueBinding ="{Binding EstimatedTotalPower}"/>
                    </DVC:Chart.Series>
                </DVC:Chart>
</Grid>

但据我所知,绑定是错误的。任何帮助?

1 个答案:

答案 0 :(得分:0)

首先,依赖属性仅对DependencyObject后代有意义,而您的视图模型不是DependencyObject

第二,如果你想扩展现有依赖项对象(在你的情况下是Chart)的功能,你应该使用attached属性 - 它们可以附加到DO和数据绑定到您的视图模型。