The key here is to set up your components router.php file (which should be found in the root folder of your component on the front end) with logic that will search for and select the appropriate menu item. I would love to see this automatically happen, but as far as I know this isn't the case.
It would probably be best to work this block of code in to some type of helper function that can be used to automatically find the best fit menu item for the content.
Here is the code that I have used in several of my custom components to get the best fit menu item:
// I use this first empty array to avoid having unset properties in my query
$base_array = array('Itemid'=>'', 'option'=>'', 'view'=>'', 'layout'=>'', 'id'=>'');
$app =& JFactory::getApplication();
$menu = $app->getMenu();
$active = $menu->getActive();
// hack to protect the actual current item as well as the search module or other places that use JRoute::_('index.php');
if (count($query)==2 && isset($query['option']) && isset($query['Itemid']) && $query['option'] && $query['Itemid']) {
return $segments;
}
// start with no match found
$match = false;
$match_level = 0;
$query += $base_array;
// we want to find a menu item for this if possible. If the active menu item is the current menu item then we should see if there is a better match.
if (empty($query['Itemid']) || ($query['Itemid'] == $active->id && empty($query['task']))) {
// load all menu items
$items = $menu->getMenu();
// use the current item over others if it ties for the best match
if ($active->query['option'] == $query['option']) {
$match_level = 1;
$match = $active;
if ($active->query['view'] == $query['view']) {
$match_level = 2;
if ($active->query['layout'] == $query['layout'] || ($query['layout']=='default' && !$active->query['layout'])) {
$match_level = 3;
if ($active->query['id'] == $query['id']) {
$match_level = 4;
}
}
}
}
// loop through each menu item in order
foreach ($items as $item) {
$item->query += $base_array;
// base check is that it is for this component
// then cycle through each possibility finding it's match level
if ($item->query['option'] == $query['option']) {
$item_match = 1;
if ($item->query['view'] == $query['view']) {
$item_match = 2;
if (!$query['layout'] && $item->query['layout']) {
$query['layout'] = 'default';
}
if ($item->query['layout'] == $query['layout'] || ($query['layout']=='default' && !$item->query['layout'])) {
$item_match = 3;
if ($item->query['id'] == $query['id']) {
$item_match = 4;
}
}
}
}
// if this item is a better match than our current match, set it as the best match
if ($item_match > $match_level) {
$match = $item;
$match_level = $item_match;
}
}
// if there is a match update Itemid to match that menu item
if ($match) {
$query['Itemid'] = $match->id;
$menuItem = $menu->getItem($match->id);
} else {
$menuItem = $menu->getActive();
}
}
All of this is kind of a mess (and I would love improvements if anyone has them!), but it gets the job done. If the current menu item is the best match, it will always stick with that.
Otherwise it should find the best match based on Component name -> view name -> layout name -> id value. The further to the right it matches, the better I deem the match!