【问题标题】:Infragistics XamWebChart Series Fill Color BindingInfragistics XamWebChart 系列填充颜色绑定
【发布时间】:2011-03-22 22:00:57
【问题描述】:

我有一个包含 XamWebChart 的 ControlTemplate。对于我的系列中创建的每个饼图,我希望其填充颜色绑定到值和标签的来源。

我的饼图的当前 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>

绑定到这个 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 元素。

我遇到的唯一问题是绑定 GeographyDataDetailItem 的 ForegroundColor 属性。 IColorProvider 到 Pie 数据系列的 Fill 属性。我不知道该怎么做。

【问题讨论】:

    标签: c# silverlight silverlight-4.0 charts infragistics


    【解决方案1】:

    我无法在 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 !-->
    ...
    

    还有后面的代码:

      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 中有一个替代方法,以减少这种代码隐藏;不过,这暂时解决了我的问题。

    【讨论】:

      猜你喜欢
      • 2012-08-27
      • 1970-01-01
      • 1970-01-01
      • 2012-07-10
      • 2019-05-27
      • 2016-07-15
      • 1970-01-01
      • 2012-04-24
      • 1970-01-01
      相关资源
      最近更新 更多