2013-07-17 150 views
3

嗨,大家好,我的組合框綁定了谷歌搜索結果。WPF:在組合框中使用屬性字段值而不是對象名稱

<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> 

我的模型看起來像這樣

public class Model_SuggestedQueries : ViewModelBase 
{ 
    private string _Query = string.Empty; 
    public string Query 
    { 
     get { return _Query; } 
     set 
     { 
      if (_Query != value) 
      { 
       _Query = value; 
       base.RaisePropertyChanged("Query"); 
      } 
     } 
    } 

    private int _Index = 0; 
    public int Index 
    { 
     get { return _Index; } 
     set 
     { 
      if (_Index != value) 
      { 
       _Index = value; 
       base.RaisePropertyChanged("Index"); 
      } 
     } 
    } 

    private string _URL = 0; 
    public string URL 
    { 
     get { return _URL; } 
     set 
     { 
      if (_URL != value) 
      { 
       _URL = value; 
       base.RaisePropertyChanged("URL"); 
      } 
     } 
    } 

    private string _Icon = 0; 
    public string Icon 
    { 
     get { return _Icon; } 
     set 
     { 
      if (_Icon != value) 
      { 
       _Icon = value; 
       base.RaisePropertyChanged("Icon"); 
      } 
     } 
    } 
} 

但是,當我做出選擇,爲.text場這個樣子。

enter image description here

我怎樣才能顯示「查詢」值,而不是對象名稱?

+0

嘗試使用'<組合框的DisplayMemberPath = 「查詢」 ... />'。這種幫助? –

回答

4

您是否嘗試將DisplayMemberPath屬性添加到您的ComboBox控件?

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

如果不工作,你可以嘗試重寫你的Model_SuggestedQueries類的的ToString()方法

+0

重寫ToString()方法。 DisplayMemberPath不歸功於ItemTemplate。謝謝! –

3

TextSearch.TextPath="Query"添加到您的ComboBox標記中。

參見MSDN Textsearch.Textpath

+0

這也適用!它比覆蓋ToString()更多的XAMLity。 –

相關問題