Friday, January 11, 2013

WPF DisplayMemberPath vs ItemTemplate Issue

Resolution for "Cannot set both DisplayMemberPath and ItemTemplate"


When we are not using ItemTemplate for Combox, then setting the following works fine for display selected desired value on the text box section of the combo box.


<ComboBox 
      Name="comboBox1" 
      IsEditable="True"     
      DisplayMemberPath="Name"

></ComboBox>


However this does not work when a ItemTemplate is specified. The following compile time error will be displayed:

    "Cannot set both DisplayMemberPath and ItemTemplate"

But if we set TextSearch.TextPath instead of DisplayMemberPath when using with ItemTemplate, it works fine.

 



<ComboBox 

      Name="comboBox1" 
      IsEditable="True"  
      ItemTemplate="{StaticResource myTemplt}"         

      TextSearch.TextPath="Name"


></ComboBox>





[note: for XmlDataProvider we will need to use @Name instead of Name if Name is an attribute]


4 comments:

  1. Thats interesting,
    How can I search for multi paths?
    For example I setup my combobox when is dropdown open to show item name and item description, but when I search I want it to search for both properties, is that possible?

    ReplyDelete
  2. TextSearch.TextPath didn't work for me.
    I have a ComboBox with an ItemTemplate consisting of an image, text(name) and a (non displayed) ID.
    I have set SelectedValuePath to "ID". SelectedValue is databound. When i choose an item manually TextPath works. But when the initial value is provided by the binding it displays the objects ToString() result. DisplayMemberPath worked fine (except the error messages in vs output window).
    So i have overridden the ToString() method to only display the name ... is there a better approach?

    ReplyDelete