绑定到Xceed图表

时间:2014-08-30 07:35:12

标签: c# wpf xaml binding

我试图绑定到Xceed中的图形控件(Wpf toolkit extended plus)。

然而,the documentation 让我更加困惑......

它说

  

可以使用DataPoints属性直接设置系列的数据点,但也可以通过将Series类的DataPointsSource属性绑定到源,然后使用BindingInfo对象填充DataPointBindings来设置它们,BindingInfo对象存储a的绑定信息DataPoint类的单个属性。使用BindingInfo.Binding属性为给定的DataPoint属性(即Content,Label,X或Y)设置Binding对象,并使用BindingInfo.PropertyName属性(其类型为DataPointPropertyName)来设置要使用绑定的属性。

所以,我的XAML是

<StackPanel.Resources>
  <loc:BackupResultsViewModel x:Key="MyVm" />
  <CollectionViewSource x:Key="StatusCollection" Source="{Binding Source={StaticResource MyVm}, Path=MyData}" />
</StackPanel.Resources>

<xctk:Chart Height="30" Width="300" ShowLegend="True" >
  <xctk:Chart.Legend>
    <xctk:Legend Dock="Left" AllowResize="False" AllowDock="True" AllowMove="True" Title="Legend"/>
  </xctk:Chart.Legend>

  <xctk:Chart.Areas>
    <xctk:Area Title="Overall status" >
      <xctk:Area.XAxis>
        <xctk:Axis ShowAxisLabel="False" ShowTickLabels="False" ShowTicks="False"/>
      </xctk:Area.XAxis>

      <xctk:Area.YAxis>
        <xctk:Axis  ShowAxisLabel="False" ShowTickLabels="False" ShowTicks="False"/>
      </xctk:Area.YAxis>

      <xctk:Area.Series>
        <xctk:Series Title="Overall status" DataPointsSource="{Binding Source={StaticResource StatusCollection}}">
          <xctk:Series.Layout>
            <xctk:PieLayout />
          </xctk:Series.Layout>

          <xctk:Series.DataPointBindings>
            <xctk:BindingInfo PropertyName="Y">
              <xctk:BindingInfo.Binding>
                <Binding/>
              </xctk:BindingInfo.Binding>
            </xctk:BindingInfo>

            <xctk:BindingInfo PropertyName="X">
              <xctk:BindingInfo.Binding>
                <Binding/>
              </xctk:BindingInfo.Binding>
            </xctk:BindingInfo>

            <xctk:BindingInfo PropertyName="Label">
              <xctk:BindingInfo.Binding>
                <Binding/>
              </xctk:BindingInfo.Binding>
            </xctk:BindingInfo>
          </xctk:Series.DataPointBindings>

        </xctk:Series>
      </xctk:Area.Series>
    </xctk:Area>
  </xctk:Chart.Areas>
</xctk:Chart>

我背后的代码是

public GraphViewModel()
{
  this.Title = title;

  var data = new DataPointsList<DataPoint>();
  data.Add(new DataPoint(0, 8, "0"));
  data.Add(new DataPoint(1, 4, "1"));
  data.Add(new DataPoint(2, 3, "2"));
  data.Add(new DataPoint(3, 2, "3"));

  this.MyData = data;        
}

public DataPointsList<DataPoint> MyData { get; set; }

图表显示,但从不包含任何数据。

那么,我如何才能使用我的图表进行绑定?

1 个答案:

答案 0 :(得分:2)

这样做

  <!-- ... -->

  <xctk:BindingInfo PropertyName="Y">
    <xctk:BindingInfo.Binding>
      <Binding Path="Y"/> <!--NOTE-->
    </xctk:BindingInfo.Binding>
  </xctk:BindingInfo>

  <xctk:BindingInfo PropertyName="X">
    <xctk:BindingInfo.Binding>
      <Binding Path="X"/><!--NOTE-->
    </xctk:BindingInfo.Binding>
  </xctk:BindingInfo>

  <xctk:BindingInfo PropertyName="Label">
    <xctk:BindingInfo.Binding>
      <Binding Path="Label"/><!--NOTE-->
    </xctk:BindingInfo.Binding>
  </xctk:BindingInfo>
</xctk:Series.DataPointBindings>