2012-04-18 21 views
0

我用我的應用程序組合框,我帶班填充它是這樣的組合框項模板:WPF選定值和搜索功能

namespace Foo.Bar{ 
    public class Item 
    { 
      public string lastName; 
      public string firstName; 
      public Foo theMeatyPart; 
    } 
} 

我可以用「姓氏填充下拉列表中, firstName「使用itemTamplate,但選定的值顯示爲」Foo.Bar.Item「。我怎樣才能將相同的模板應用到selectedItem中,並且使搜​​索功能正常工作而不會覆蓋Item的ToString方法?

這裏是XAML:

<Style x:Key="SearchComboStyle" TargetType="ComboBox"> 
       <Style.Setters> 
        <Setter Property="Width" Value="150"></Setter> 
       </Style.Setters> 
      </Style> 
      <DataTemplate x:Key="SearchComboItemTemplate" > 
        <TextBlock DataContext="{Binding}"> 
          <TextBlock.Text> 
           <MultiBinding StringFormat="{}{0}, {1}"> 
           <Binding Path="lastName"/> 
           <Binding Path="firstName"/> 
           </MultiBinding> 
          </TextBlock.Text> 
        </TextBlock> 
      </DataTemplate> 


<ComboBox ItemTemplate="{StaticResource SearchComboItemTemplate}" Style="{StaticResource SearchComboStyle}" 
        ItemsSource="{Binding Path=PhysiciansList, RelativeSource={RelativeSource AncestorType=local:ExamViewerControl, AncestorLevel=1}}"  IsTextSearchEnabled="True" IsTextSearchCaseSensitive="False" IsEditable="True" TextSearch.TextPath="Person.LastName" />   
+0

你可以發佈xaml嗎? – dcarneiro 2012-04-18 14:03:48

回答

2

UPD:看起來你需要設置SelectionBoxItemTemplate

您可以使用DisplayMemberPathTextSearch.TextPath在不修改ToString()的情況下啓用搜索。

+1

SelectionBoxItemTemplate是隻讀的 – PhilBrown 2012-04-18 14:24:18

+0

Ooops。抱歉。你使用什麼樣的ComboBox?它是可編輯的嗎? – 2012-04-18 14:32:17

+0

System.Windows.Controls.ComboBox .NET 4.0 – PhilBrown 2012-04-18 14:41:17