简体   繁体   English

Windows Phone 8.1 RT-ItemTemplateSelector-不绑定全模式

[英]Windows Phone 8.1 RT - ItemTemplateSelector - Not Binding the Fullmode

I am developing Windows phone 8.1 RT app. 我正在开发Windows Phone 8.1 RT应用程序。 I am using Combobox. 我正在使用Combobox。 There are more than 20 Operators to be bind to the Combbox. 有超过20个运算符绑定到Combbox。 when we tab on the Combobox, App should open the all 20 opetaros in full mode. 当我们在“组合框”上使用Tab键时,App应该以完整模式打开所有20个opetaros。 I need two different templates, should use one template (image and textblock) when items opens in Full mode and other template (only TextBlock) when a item selected among full mode items. 我需要两个不同的模板,当在完全模式下打开项目时,应使用一个模板(图像和文本块),而在完全模式下选择一个项目时,则应使用另一个模板(仅文本块)。 DataTemplateSelector is inherited and created new DataTemplateSelector. DataTemplateSelector被继承并创建新的DataTemplateSelector。 ItemTemplateSelector is assigned with newly inherited DataTemplateSelector. 为ItemTemplateSelector分配了新继承的DataTemplateSelector。 Below the is code used. 下面是使用的代码。

<ComboBox Grid.Row="3" Grid.Column="0" Margin="15 5 0 0" 
      ItemsSource="{Binding Operators}"  SelectedItem="{Binding SelectedOperator, Mode=TwoWay}"
      Style="{StaticResource FullModeComboBoxStyle1}"   ItemContainerStyle="{StaticResource FullModeComboBoxItemStyle1}"
      VerticalAlignment="Top"
      Height="65"
      ItemTemplateSelector="{StaticResource ExploreTemplateSelector}"
      />

TemplateSelector TemplateSelector

public class ExploreTemplateSelector : DataTemplateSelector
{
    public DataTemplate DropdownItemsTemplate { get; set; }
    public DataTemplate SelectedItemTemplate { get; set; }

    protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
    {
        var parent = container;

        while (parent != null && !(parent is ComboBoxItem) && !(parent is ComboBox))
            parent = VisualTreeHelper.GetParent(parent);

        var inDropDown = (parent is ComboBoxItem);

        return inDropDown
            ? DropdownItemsTemplate
            : SelectedItemTemplate;

    }
}

DataTemplate 数据模板

<DataTemplate x:Key="OperatorDataTemplate">
            <StackPanel Orientation="Horizontal" Margin="5 5 0 0" Height="Auto">
                <Image Source="{Binding ImageUri}" Height="35" Width="60" VerticalAlignment="Top" />
                <TextBlock Text="{Binding Name}" Style="{StaticResource ComboboxTextBlockStyle}" Margin="5 0 0 0"   Width="120" VerticalAlignment="Top" TextWrapping="Wrap"/>
            </StackPanel>
        </DataTemplate>

        <DataTemplate x:Key="SelectedOperatorDataTemplate">
            <TextBlock Text="{Binding Name}" Style="{StaticResource ComboboxTextBlockStyle}" Margin="5 0 0 0"   Width="120" VerticalAlignment="Top" TextWrapping="Wrap"/>
        </DataTemplate>

        <class:ExploreTemplateSelector x:Key="ExploreTemplateSelector" DropdownItemsTemplate="{StaticResource SelectedOperatorDataTemplate}"
                                       SelectedItemTemplate="{StaticResource SelectedOperatorDataTemplate}"
                                       />

Items are not binding when we tab the Combobox, showing list of namespace. 当我们选择组合框时,项目未绑定,显示名称空间列表。 But selecting a item in full mode, SelectTemplateCore is hitted and Selected item is showed using SelectedItemTemplate. 但是以完全模式选择一个项目时,将选中SelectTemplateCore并使用SelectedItemTemplate显示选定的项目。 But SelectTemplateCore is not hitted when binding Datasource. 但是,绑定数据源时,不会选中SelectTemplateCore。

What is the problem in this code? 这段代码有什么问题? Why DropDownItemsTemplate is not used to bind the items? 为什么不使用DropDownItemsTemplate绑定项目?

Thanks in advance 提前致谢

因为在xaml中指定模板选择器时,属性是使用相同的数据模板“ SelectedOperatorDataTemplate”初始化的

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

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