12117

Why does the Combobox.FindName() method always return null?

Question:

I have defined Combobox's ItemSource in List object. I want to reach the ComboBoxItem by using FindName() method but it always returns null. I have tried ApplyTemplate() at the beginning and I also have tried to reach the Item using Combobox.Template. Here is my code. Any suggestions?

List<string> subjectsList = e.Result; cbCategory.ItemsSource = subjectsList; cbCategory.SelectedItem = cbCategory.FindName("DefaultChatSubject");

By the way, I do not have any problem about the Items in ItemSource.

Answer1:

The <a href="http://msdn.microsoft.com/en-us/library/system.windows.frameworktemplate.findname%28v=vs.110%29.aspx" rel="nofollow">FrameworkTemplate.FindName Method</a> <em>Finds an element that has the provided identifier name</em>. From the linked page on MSDN:

<blockquote>

If the element has child elements, these child elements are all searched recursively for the requested named element.

FindName operates within the current element's namescope. For details, see <a href="http://msdn.microsoft.com/en-us/library/ms746659.aspx" rel="nofollow">WPF XAML Namescopes</a>.

</blockquote>

In order to use the FindName method successfully, the child element that you are looking for <em>must</em> have their Name property set. As it is somewhat unlikely that a data bound collection of items will have the ComboBoxItem.Name property set, it is also unlikely that this will work for you.

A better way to set the selected item is like this:

cbCategory.SelectedItem = subjectsList.First(i => i.Property == "DefaultChatSubject");

Or if your collection items are just strings, like this:

cbCategory.SelectedItem = "DefaultChatSubject";

Answer2:

FindName is meant to find a named child element of a FrameworkElement. It does not find an item string in the Items collection of an ItemsControl (like your ComboBox).

You could simply call this instead:

cbCategory.SelectedItem = "DefaultChatSubject";

Recommend

  • C# InvokeRequired with property getter
  • Enable text box based on radio button selected
  • DataGrid: Help accessing a control defined in a CellEditingTemplate
  • How to get instance of the GridControl in LookupEdit (Devexpress)
  • Return list instead of an item Webservice Phone 8.1 UAP
  • Change Font Style (bold, Italic, bold italic) in C# [duplicate]
  • How to use a MultiBinding on DataGridTextColumn?
  • Add items to ComboBox at runtime?
  • Binding ContextMenu Tag to Owner
  • Binding a list with another list with WPF
  • How to set my toolbar fixed while scrolling android
  • Apache 2.4 - remove | delete | uninstall
  • Numpy divide by zero. Why?
  • AT Commands to Send SMS not working in Windows 8.1
  • php design question - will a Helper help here?
  • How to delete a row from a dynamic generate table using jquery?
  • Python: how to group similar lists together in a list of lists?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • AngularJs get employee from factory
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Codeigniter doesn't let me update entry, because some fields must be unique
  • Free memory of cv::Mat loaded using FileStorage API
  • Trying to get generic when generic is not available
  • Getting Messege Twice Using IMvxMessenger
  • Change div Background jquery
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • apache spark aggregate function using min value
  • Bitwise OR returns boolean when one of operands is nil
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Busy indicator not showing up in wpf window [duplicate]
  • Sorting a 2D array using the second column C++
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal