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

C# 如何在silverlight中将集合传递给绑定转换器参数,c#,silverlight,xaml,collections,converter,C#,Silverlight,Xaml,Collections,Converter,在c代码中的silverlight应用程序中,我从WCF RIA服务获取数据。 然后我想把这个数据(列表)传递给图表(System.Windows.Forms.DataVisualization.Charting.chart)轴转换器参数 this.specialDayClient = new SpecialDayClient(); this.specialDayClient.SpecialDayLoaded += new EventHandler(specialDayC

在c代码中的silverlight应用程序中,我从WCF RIA服务获取数据。 然后我想把这个数据(列表)传递给图表(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>();
        }
this.specialDayClient=new specialDayClient();
this.specialDayClient.SpecialDayLoaded+=新事件处理程序(specialDayClient\u SpecialDaysLoaded);
this.specialDayClient.SpecialDayLoadFailed+=新事件处理程序(specialDayClient\u SpecialDaysLoadFailed);
私有void specialDayClient\u SpecialDaysLoaded(对象发送方,事件参数e)
{
specialDays=发件人为IEnumerable;
var绑定=新绑定(“转换器绑定”)
{
Converter=新的DateToColorConverter(),
转换器参数=特殊日期
};
var setter=新setter(ForegroundProperty,binding);
((DateTimeAxis)chartCashRemainders.axis[0]).AxisLabelStyle.Setters.Add(setter);
//在这一行之后,我收到错误消息“调用COM组件时返回了error HRESULT E_FAIL。”
}
私有void SpecialDaySloadClient\u SpecialDaysLoadFailed(对象发送方,事件参数e)
{
specialDays=新列表();
}
((DateTimeAxis)chartCashRemainders.Axes[0]).AxisLabelStyle.Setters.Add(setter)之后我收到错误消息“调用COM组件返回错误HRESULT E_FAIL。”


我的错误在哪里?

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

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);

            }
        };
    }
} 
公共类特殊日期:列表
{
公共特别日
{
if(DesignerProperties.IsInDesignTool)
返回;
DeviceManagement domainContext domainContext=新设备管理domainContext();
var query=domainContext.getSpecialDaysForditorry();
LoadOperation=domainContext.Load(查询);
操作完成+=(s,e)=>
{
if(operation.HasError)
{
if(operation.Error!=null)
{
操作。MarkErrorAsHandled();
}
this.AddRange(新列表());
}
其他的
{
List specialDays=operation.Entities.ToList();
此.AddRange(特殊日期);
}
};
}
} 
然后在xaml代码中添加此类(到):


并将作为静态资源添加到转换器:

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

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