是否可以绑定到在Silverlight中的lambda前pression? [英] Is it possible to bind to a lambda expression in Silverlight?

查看:102
本文介绍了是否可以绑定到在Silverlight中的lambda前pression?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的绑定到一个集合列表框。该系列有一个子集合(StepDatas)。我想绑定到子集合的计数,但用WHERE语句。我可以绑定到ChildCollection.Count但需要添加拉姆达前pression时迷路。这里的XAML:

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 statement. I can bind to ChildCollection.Count but get lost when needing to add the lambda expression. Here's the XAML:

<ListBox Height="Auto" Style="{StaticResource ListBoxStyle1}" Margin="4,46,4,4" x:Name="lstLeftNavigation" Background="{x:Null}" SelectionChanged="lstLeftNavigation_SelectionChanged">
<ListBox.ItemTemplate>
    <DataTemplate>
		<Grid Width="180" Margin="2,2,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" d:LayoutOverrides="Width" MinHeight="36">
			<TextBlock Text="{Binding StepNm}" x:Name="tbStepNm" Margin="10,0,34,0" TextWrapping="Wrap" FontFamily="Portable User Interface" Foreground="White" FontSize="10" FontWeight="Bold" VerticalAlignment="Center"/>
			<Image Height="37" HorizontalAlignment="Right" Margin="0" VerticalAlignment="Center"  Width="37" Source="Images/imgIcoChecked.png" Stretch="Fill"/>
		</Grid>
    </DataTemplate>
</ListBox.ItemTemplate>

以上的作品结合到子集合的计数。不过,我希望显示孩子集合,其中一个条件满足的计数。在这种特殊情况下,孩子集合有一个建成物业(布尔)。所以...我想显示计数StepDatas.Where(X => x.Completed == true)而.Count之间。

The above works to bind to the count of the child collection. However I wish to show a count of the child collection where a certain condition is met. In this specific case, the child collection has a completed property (bool). So...I want to show the count StepDatas.Where(x => x.Completed == true).Count.

这是在任何可能的方式?感谢您的帮助!

Is this in any way possible? Thanks for any help!

推荐答案

简短的回答这个问题问题是:没有

The short answer to the subject question is: no.

明智的答案是:确保计数则需要由数据模型可用的属性。例如,确保 StepDatas 暴露的类型有一个计数属性。

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.

然而,你做,在任何可能的方式?这个资格。它可以绑定到列表项数据上下文,并使用一些值转换器疯狂来执行你的lambda。然而,为了让事情变得简单,你需要创建一个特定的转换器为你的lambda。
这里是转换器code将如下所示: -

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

您将在使这个转换器在XAML中的资源属性缴费的一个实例,说方便了用户控件: -

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

<UserControl x:Class="YourNameSpace.ThisControlName"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="clr-namespace:YourNameSpace;assembly=YourAssemblyName">
  <UserControl.Resources>
    <local:CountCompletedStepDatas x:Key="Counter" />
  </UserContro.Resources>

在你现在的绑定: -

Now in your binding:-

 <TextBlock Text="{Binding Converter={StaticResource Counter} }" ... >

这篇关于是否可以绑定到在Silverlight中的lambda前pression?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆