Is it possible to bind to a lambda expression in Silverlight?

前端 未结 2 1118
眼角桃花
眼角桃花 2021-01-22 15:21

I have a listbox that simply binds to a collection. The collection has a child collection (StepDatas). I would like to bind to a count of the child collection but with a WHERE

2条回答
  •  暖寄归人
    2021-01-22 16:10

    The short answer to the subject question is: no.

    The sensible answer is: Ensure the Count you need is made available a property of the data model. E.g., ensure the type exposed by StepDatas has a Count property.

    However you do qualify this with "in any way possible?". It is possible to bind to the ListItem data context and using some value converter madness to execute your lambda. However to keep things simple you need to create a converter specifically for your lambda. Here is what the converter code would look like:-

     public class CountCompletedStepDatas : IValueConverter
     {
    
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
          YourItemsType item = (YourItemsType)value;
          return item.StepDatas.Were(x => x.Completed == true).Count().ToString(culture);
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
          throw new NotImplementedException();
        }
      }
    

    You would the make an instance of this converter avaiable in a Resources property in the XAML, say of convenience in the UserControl:-

    
      
        
      
    

    Now in your binding:-

     
    

提交回复
热议问题