如何在silverlight中将集合传递给绑定转换器参数

时间:2014-01-08 12:18:41

标签: c# silverlight xaml collections converter

在c#代码中的silverlight应用程序中,我从WCF RIA服务中获取数据。 然后我想将此数据(List)传递给图表(System.Windows.Forms.DataVisualization.Charting.Chart)轴转换器参数。

this.specialDayClient = new SpecialDayClient();
            this.specialDayClient.SpecialDayLoaded += new EventHandler(specialDayClient_SpecialDaysLoaded);
            this.specialDayClient.SpecialDayLoadFailed += new EventHandler(specialDayClient_SpecialDaysLoadFailed);

        private void specialDayClient_SpecialDaysLoaded(object sender, EventArgs e)
        {
            specialDays = sender as IEnumerable<SpecialDay>;
            var binding = new Binding("ConverterBinding")
            {
                Converter = new DateToColorConverter(),
                ConverterParameter = specialDays
            };

        var setter = new Setter(ForegroundProperty, binding);


            ((DateTimeAxis)chartCashRemainders.Axes[0]).AxisLabelStyle.Setters.Add(setter);
            //After this row I get error message "Error HRESULT E_FAIL has been returned from a call to a COM component."
        }

        private void specialDayClient_SpecialDaysLoadFailed(object sender, EventArgs e)
        {
            specialDays = new List<SpecialDay>();
        }

((DateTimeAxis)chartCashRemainders.Axes [0])。AxisLabelStyle.Setters.Add(setter); 我收到错误消息“错误HRESULT E_FAIL已从调用返回到COM组件。“

我的错误在哪里?

1 个答案:

答案 0 :(得分:1)

好的,最简单的方法 - 使用对象集合创建一个新类:

public class SpecialDays : List<SpecialDay>
{
    public SpecialDays()
    {
        if(DesignerProperties.IsInDesignTool)
            return;

        DeviceManagementDomainContext domainContext = new DeviceManagementDomainContext();

        var query = domainContext.GetSpecialDaysForEditorQuery();
        LoadOperation<SpecialDay> operation = domainContext.Load(query);
        operation.Completed += (s, e) =>
        {
            if (operation.HasError)
            {
                if (operation.Error != null)
                {
                    operation.MarkErrorAsHandled();
                }
                this.AddRange(new List<SpecialDay>());
            }
            else
            {
                List<SpecialDay> specialDays = operation.Entities.ToList();
                this.AddRange(specialDays);

            }
        };
    }
} 

然后在xaml代码(to)中添加此类:

   <UserControl.Resources>
        <bs2Models:SpecialDays x:Key="SpecialDays"/>
    </UserControl.Resources>

并添加为转换器的静态资源:

<Style x:Key="HorizontalAxisLabelStyle" TargetType="toolkit:DateTimeAxisLabel">
            <Setter Property="Foreground" Value="{Binding Converter={StaticResource DateToColorConverter}, ConverterParameter={StaticResource SpecialDays}}"/>
</Style>