【问题标题】:How to pass collection to binding converter parameter in silverlight如何将集合传递给 Silverlight 中的绑定转换器参数
【发布时间】:2014-01-26 13:01:24
【问题描述】:

在 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 组件。”

我的错误在哪里?

【问题讨论】:

    标签: c# silverlight xaml collections converter


    【解决方案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代码中添加这个类(到):

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

    【讨论】:

      猜你喜欢
      • 2022-01-19
      • 1970-01-01
      • 2011-08-07
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多