我一直在寻找用于sivleright的DataTemplateSelectors的示例,并且从示例中我已经看到在Content Control中返回并评估的值似乎是单个属性已从集合中返回到列表框中。Silverlight如何在绑定到复杂集合时使用列表框项目的数据模板选择器
有没有人有任何建议如何解决此问题时返回一个复杂的集合到列表框项目源?通过复杂的集合,我的意思是一个名为“Result”的可观察集合,其内部存在名为“Names”的List集合?我需要从List集合中找到的属性中触发数据模板选择器,而不是结果集合中的属性。即列表集合可能包含属性名称,性别,年龄,我想要使用性别属性作为触发器,而结果可能包含属性,如属性,时间或笔记,我不需要使用这些来调用模板触发器。
谢谢你的任何建议。
更新与代码示例
通过这个我在哪里卡住工作是在我的列表框项自定义模板的结合。 这里是ListBox控件XAML
<ListBox HorizontalAlignment="Stretch" ItemsSource="{Binding SearchResults[0].Results, Mode=TwoWay}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" BorderBrush="#66C4C4C4" >
<ListBox.ItemTemplate>
<DataTemplate>
<qr:SearchResultItemControl />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
QR:SerarchResultItemContol XAML是我目前已经定义了这个用户控件自定义lsitbox项目包含下列元素
<Grid x:Name="LayoutRoot">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="92.915"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Rectangle Fill="#FFF4F4F5" Margin="5,6,7.5,0" RadiusY="4" RadiusX="4" Stroke="Black" Height="52" VerticalAlignment="Top"/>
<TextBlock Margin="4,3,76,0" TextWrapping="Wrap" FontSize="13.333" Height="17" VerticalAlignment="Top" Grid.Column="1" d:LayoutOverrides="VerticalAlignment" FontWeight="Bold" Text="{Binding Type, Mode=TwoWay}"/>
<TextBlock Margin="8,15,10.5,0" TextWrapping="Wrap" FontSize="9.333" Height="35" VerticalAlignment="Top" d:LayoutOverrides="VerticalAlignment" Text="image placeholder" TextAlignment="Center"/>
<TextBlock Margin="3,97,4,0" TextWrapping="Wrap" FontSize="12" Height="17" VerticalAlignment="Top" Text="{Binding Relevance, Mode=TwoWay}"/>
<TextBlock Margin="3,75,20,0" TextWrapping="Wrap" FontSize="12" Height="18" VerticalAlignment="Top" Width="70" Text="{StaticResource txtRelevance}"/>
<TextBlock Grid.Column="1" Margin="4,24,5,6" TextWrapping="Wrap" FontSize="12" RenderTransformOrigin="0.5,0.5" Text="{Binding HitContext, Mode=TwoWay}"/>
</Grid>
运行该控制工作正常,我看我的结果。我想要做的是根据Type绑定中返回的值来控制ListboxItem模板的加载数据。
itemsSource正在从我的视图模型中返回正在从WcF服务生成的SearchResults集合。对于绑定收集
VM代码:
public ObservableCollection<QueryResponse> SearchResults
{
get
{
return this._SearchResults;
}
private set
{
if (this._SearchResults == value)
return;
// Set the new value and notify
this._SearchResults = value;
this.NotifyPropertyChanged("SearchResults");//this binds to UI search returns
}
}
的信息搜索结果的收集是由以下属性
信息搜索结果 串QUERYTEXT的 串QueryTime 结果 串TotalMatches ...
在SearchResult中返回的结果集合包含: 字符串内容 串HitContext 串ID 字符串相关 字符串类型
我试图做到的是有DataTemplateSelection而引发从结果集返回的类型值。
任何建议,将不胜感激。
发布您的集合和类的代码。 DataTemplateSelecor类可以像'currentResultItem.Names.Any(n => n.Name =“...”)''这样的代码,所以我认为根据您的示例调整它并不困难。 – vorrtex 2011-05-12 12:03:34