简体   繁体   English

WPF ComboBox ItemTemplateSelector.Selecttemplate永远不会被调用

[英]WPF ComboBox ItemTemplateSelector.Selecttemplate is never called

Based on this answer I tried to use the following code to achieve a ComboBox with different templates depending on whether or not the drop down is open. 基于此答案,我尝试使用以下代码来实现具有不同模板的ComboBox ,具体取决于下拉列表是否打开。 The ComboBox definition looks like this: ComboBox定义如下所示:

<ComboBox SelectedValuePath="Id"
          DisplayMemberPath="Name">
    <ComboBox.Resources>
        <DataTemplate x:Key="SelectedTemplate">
            <TextBlock Text="Abbreviation" />
        </DataTemplate>

        <DataTemplate x:Key="DropDownTemplate">
            <ContentControl Content="{Binding}">
                <ContentControl.Style>
                    <Style TargetType="{x:Type ContentControl}">
                        <Setter Property="ContentTemplate">
                            <Setter.Value>
                                <DataTemplate>
                                    <Grid Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Border}}, Path=ActualWidth, Mode=OneTime}">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="*" />
                                            <ColumnDefinition Width="20" />
                                        </Grid.ColumnDefinitions>

                                        <TextBlock Grid.Column="0"
                                                   Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ComboBox}}, Path=DisplayMemberPath}" />
                                        <TextBlock Grid.Column="1" 
                                                   Text="Abbreviation" />
                                    </Grid>
                                </DataTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </ContentControl.Style>
            </ContentControl>
        </DataTemplate>

        <infrastructure:ComboBoxItemTemplateSelector x:Key="ComboBoxItemTemplateSelector"
                                                 DropDownDataTemplate="{StaticResource DropDownTemplate}"
                                                 SelectedDataTemplate="{StaticResource SelectedTemplate}" />
    </ComboBox.Resources>
</ComboBox>

and the ComboBoxItemTemplateSelector looks like this: ComboBoxItemTemplateSelector看起来像这样:

public class ComboBoxItemTemplateSelector : DataTemplateSelector
{
    public DataTemplate DropDownDataTemplate { get; set; }
    public DataTemplate SelectedDataTemplate { get; set; }

    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        return DependencyObjectHelper.GetVisualParent<ComboBoxItem>(container) != null ? this.DropDownDataTemplate : this.SelectedDataTemplate;
    }
}

Now the problem is that somehow SelectTemplate is never called , even although I checked all the DynamicResource s and StaticResource s. 现在的问题是,即使我检查了所有DynamicResourceStaticResource ,也始终无法调用SelectTemplate

You need to assign the ComboBoxItemTemplateSelector to the ComboBox like this: 您需要像这样将ComboBox分配给ComboBoxItemTemplateSelector

<ComboBox ItemTemplateSelector="{DynamicResource ComboBoxItemTemplateSelector}">

and you cannot set the DisplayMemberPath or the SelectTemplate method will never be called. 并且您无法设置DisplayMemberPathSelectTemplate将永远不会调用SelectTemplate方法。

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

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