2011-03-22 101 views
1

我有一個ControlTemplate它包含一個XamWebChart。對於我的系列中創建的每個餅圖切片,我希望它的填充顏色綁定到Value和Label來自的相同源。Infragistics XamWebChart系列填充顏色綁定

我的餅圖目前的XAML看起來像:

<ControlTemplate x:Key="DataLocation"> 
<Viewbox 
Width="{TemplateBinding Width}" 
Height="{TemplateBinding Height}"     
Stretch="Fill"> 
<Grid> 
<Grid.Resources> 
<Style x:Key="PieChartSeriesStyle" TargetType="Chart:Series"> 
    <Setter Property="Stroke" Value="#00FFFFFF" />        
    <Setter Property="Marker"> 
    <Setter.Value> 
    <Chart:Marker Foreground="#FFFFFFFF" /> 
    </Setter.Value> 
    </Setter> 
    </Style> 
</Grid.Resources> 
<Chart:XamWebChart x:Name="PieChart" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> 
    <Chart:XamWebChart.Scene> 
    <Chart:Scene> 
    <Chart:Scene.GridArea> 
     <Chart:GridArea BorderThickness="0" Background="#00FFFFFF" /> 
    </Chart:Scene.GridArea> 
    </Chart:Scene> 
    </Chart:XamWebChart.Scene> 

<Chart:XamWebChart.Series> 
    <Chart:Series 
    ChartType="Pie" 
    Style="{StaticResource PieChartSeriesStyle}" 
    DataMapping="Value=Amount;Label=Identifier"         
    DataSource="{Binding Path=ToolTip.Details}" 
    Fill="{Binding DetailItem.ColorProvider.BackgroundColor}"> 
    </Chart:Series>         
</Chart:XamWebChart.Series>       
</Chart:XamWebChart> 
</Grid> 
</Viewbox> 
</ControlTemplate> 

綁定到這個控件模板的對象包括:

public sealed class GeographyDataItem{ 
     public IEnumerable<GeographyDataDetailItem> Details 
     { 
      get { return _details; } 
     } 

} 

隨着孩子:

public sealed class GeographyDataDetailItem 
    { 
     private readonly IColorProvider _colorProvider; 

     public IColorProvider ColorProvider 
     { 
      get { return _colorProvider; } 
     } 

     public string Identifier { get; private set; } 
     public double Amount { get; private set; } 

     public GeographyDataDetailItem(string identifier, double amount, IColorProvider colorProvider) 
     { 
      _colorProvider = colorProvider; 
      Identifier = identifier; 
      Amount = amount; 
     }   
    } 

凡IColorProvider是:

public interface IColorProvider 
{ 
    Color ForegroundColor { get; } 
    Color BackgroundColor { get; } 
} 

ControlTemplate綁定設置爲綁定到GeographyDataItem元素。

我遇到的唯一的問題是有約束力的GeographyDataDetailItemForegroundColor財產。 IColorProvider填充屬性的餡餅數據系列。我不知道如何去做這件事。

回答

2

我無法在XAML中實現我想要的功能,但能夠在控制代碼隱藏中支持此用例。

我的解決辦法是:

... 
<Chart:XamWebChart.Series> 
<Chart:Series ChartType="Pie" 
    Style="{StaticResource PieChartSeriesStyle}" 
    DataMapping="Value=Amount;Label=Identifier" 
    DataSource="{Binding Path=ToolTip.Details}" 
    Loaded="SeriesLoaded" /> 
<!-- Loaded Event Added !--> 
... 

和代碼beind:

private void SeriesLoaded(object sender, RoutedEventArgs e) 
    { 
     var series = (Series) sender; 
     ColorDataPointSeries(series); 
    } 

    /// <summary> 
    /// Colors in <see cref="DataPoint"/> from the bound DataSource for the pie chart layer control. 
    /// </summary> 
    /// <param name="series"></param> 
    private static void ColorDataPointSeries(Series series) 
    { 
     //If nothing is bound there is nothing to do. 
     if (null == series.DataSource) 
      return; 

     var dataSource = ((IEnumerable<GeographyDataDetailItem>) series.DataSource).ToList(); 
     var dataPoints = series.DataPoints; 

     //Note, I am depending on the control to have the exact number of dataPoints as dataSource elements here. 
     for (var sourceIndex = 0; sourceIndex < dataSource.Count(); sourceIndex++) 
     { 
      //Iterate through each dataSoruce, looking for a color provider. 
      //If one exists, change the Fill property of the control DataPoint. 
      var colorProvider = dataSource[sourceIndex].ColorProvider; 
      if (null != colorProvider) 
       dataPoints[sourceIndex].Fill = new SolidColorBrush(colorProvider.BackgroundColor); 
     } 
    }    

所以,現在每次XamWebChart加載的渲染,填充顏色從應用數據源。綁定聽財產的變化是沒有必要的,因爲我不希望顏色在渲染之後改變。

很高興知道在XAML中有這種替代方法,以減少這種代碼隱藏;然而,目前這解決了我的問題。