简体   繁体   English

Silverlight如何在绑定到复杂集合时如何将数据模板选择器用于列表框项目

[英]Silverlight how to use data template selector for listbox items when binding to complex collections

I have been looking at samples of DataTemplateSelectors for sivleright and from the examples I have seen the values being returned and evaluated within the Content Control seems to be a single property that has been returned to the listbox from a collection. 我一直在看sivleright的DataTemplateSelector的示例,从示例中我已经看到在Content Control中返回和评估的值似乎是一个已从集合返回到列表框的单个属性。

Does anyone have any suggestions on how this is addressed when returning a complex collection to a listbox item source? 将复杂的集合返回到列表框项目源时,没有人对如何解决此问题有任何建议吗? By complex collection I mean an observable collection named "Result" and within that a List collection exists titled "Names" ? 所谓复杂集合,是指一个名为“ Result”的可观察集合,其中存在一个名为“ Names”的List集合? I need to trigger the data template selector from a property found within the List collection and not properties from the result collection. 我需要从列表集合中找到的属性而不是结果集合中的属性触发数据模板选择器。 ie List collection may contain the properties name, sex, age and I want to use the sex property as the trigger whereas Result may contain properties like property, time, or notes and I do not need to use these to invoke the template trigger. 即列表集合可能包含属性名称,性别,年龄,并且我想使用sex属性作为触发器,而结果可能包含属性,时间或注释之类的属性,而我不需要使用这些属性来调用模板触发器。

Thank you for any suggestions. 感谢您的任何建议。

Update with code example 用代码示例更新

Working through this where I am getting stuck is in the binding of my listbox item custom template. 解决这个问题的方法是在列表框项目自定义模板的绑定中。 Here is the listbox control xaml 这是列表框控件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 is the custom lsitbox item I currently have defined this user control contains the following elements 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>

Running this the control works fine I see my results. 运行此控件效果很好,我看到了结果。 What I would like to do is control what data ListboxItem template is loaded based upon the value returned in Type binding. 我想做的是根据Type绑定中返回的值来控制加载哪些数据ListboxItem模板。

The itemsSource is being returned from my view model SearchResults Collection which is being generated from a WcF service. 我从WcF服务生成的视图模型SearchResults集合返回了itemsSource。

VM code for bindable collection: 可绑定集合的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
        }
    }

the searchresult collection is comprised of the following properties searchresult集合由以下属性组成

SearchResult string QueryText string QueryTime Results string TotalMatches ... SearchResult字符串QueryText字符串QueryTime结果字符串TotalMatches ...

The Results collection that is returned within SearchResult contains: string Content string HitContext string ID string relevance string Type SearchResult中返回的Results集合包含:字符串内容字符串HitContext字符串ID字符串相关性字符串类型

What I am trying to accomplish is to have the DataTemplateSelection triggered off of the Type value returned from the results collection. 我想要完成的是从结果集合返回的Type值触发DataTemplateSelection。

Any suggestions would be appreciated. 任何建议,将不胜感激。

Found my answer based upon the following solution from codeproject This solution provided a very basic and straight forward DataSelection template that fit my needs. 根据codeproject的以下解决方案找到了我的答案。该解决方案提供了一个非常基本且直接的DataSelection模板,可以满足我的需求。

With regards to accessing a complex collection vs. a flat collection I cast my item object against the nested collection and from there able to access the appropriate properties. 关于访问复杂集合与平面集合,我将我的item对象投射到嵌套集合上,从那里可以访问适当的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM