我一直在尋找用於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