WPF:在组合框中使用属性字段值,而不是对象名称

WPF:在组合框中使用属性字段值,而不是对象名称,wpf,wpf-controls,Wpf,Wpf Controls,我的组合框与谷歌搜索结果绑定 <ComboBox Style="{StaticResource ComboBoxStyle}" IsEditable="True" IsTextSearchEnabled="False" ItemsSource="{Binding GoogleSuggest.SuggestedQueries}" SelectedItem="{Bi

我的组合框与谷歌搜索结果绑定

<ComboBox
    Style="{StaticResource ComboBoxStyle}"
    IsEditable="True"
    IsTextSearchEnabled="False"
    ItemsSource="{Binding GoogleSuggest.SuggestedQueries}"
    SelectedItem="{Binding GoogleSuggest.SelectedQuery}"
    >
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                
                <Image Source={Binding IconPath, Converter={StaticResource IconPathToImageSource} Width="32" Height="32" />
                
                <StackPanel Grid.Column="1">
                    <TextBlock Text="{Binding Query}" Margin="0,8" FontSize="24" />
                    <TextBlock Text="{Binding URL}" Margin="0,8" FontSize="16" />
                </StackPanel>
            </Grid>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>
但是当我进行选择时,.Text字段如下所示


如何显示“查询”值而不是对象名?

是否尝试将DisplayMemberPath属性添加到ComboBox控件中

 <ComboBox
        Style="{StaticResource ComboBoxStyle}"
        IsEditable="True"
        IsTextSearchEnabled="False"
        ItemsSource="{Binding GoogleSuggest.SuggestedQueries}"
        SelectedItem="{Binding GoogleSuggest.SelectedQuery}"
        DisplayMemberPath="Query"
        >


如果不起作用,您可以尝试覆盖您的模型建议查询类的ToString()方法。

TextSearch.TextPath=“Query”
添加到组合框标记中


请参见

我认为有一种更容易实现目标的方法,请尝试以下方法: 只需重写类“Model\u SuggestedQueries”的ToString()函数
:p

尝试使用
。是否有帮助?重写ToString()方法有效。DisplayMemberPath由于ItemTemplate而不可用。谢谢这也行!它比重写ToString()更简单。
 <ComboBox
        Style="{StaticResource ComboBoxStyle}"
        IsEditable="True"
        IsTextSearchEnabled="False"
        ItemsSource="{Binding GoogleSuggest.SuggestedQueries}"
        SelectedItem="{Binding GoogleSuggest.SelectedQuery}"
        DisplayMemberPath="Query"
        >