WPF ListView arrow navigation and keystroke problem - wpf

I have a readonly WPF listView and I am having two problem with it
(1) When I load the listView, I set the selecteditem in code behind. It works fine but when I use up/dowm arrow key to navigate through the list, it always jump to the first item at start.
(2) I can't use keystroke to select item
Can anyone give me some idea about how to solve them?
Thanks

Yes, the problem is you need to call Focus() on the list item after selecting it.
But for various reasons it's not as simple as all that.
See Arrow keys don't work after programmatically setting ListView.SelectedItem
...for full details.

Related

Mimicing the tab key being pressed in Silverlight TextBox KeyDown event

Title pretty much explains it all really. I need to know how to automatically jump to the next element (the element that would get focus if the user presses the "Tab" key), whatever that may be, in the KeyDown event of a Silverlight TextBox. My TextBoxes are generated dynamically so I can't, for example, manually code TextBox1 to set focus on TextBox2 on keydown etc. Any help would be greatly appreciated
I don't believe there is an easy way to accomplish this. The only method I know of is to use the VisualTreeHelper and get all the elements and then loop through them by their tab stop values.
If your textboxes are generated dynamically you could use a Behavior to set their tab stop properties pretty easily which would be a much better solution. Or just set it when you create them.

wpf - autocompletebox doesn't update on mousedown

I have an autocompletebox that works but for one oddity I was hoping for help with. When selecting an item in the popup, using the keyboard to arrow down and then selecting it using either the enter key or with the mouse, the item is selected and updated into the autocompletebox. However, if one instead of arrowing down to the item simply hovers over it instead and select it with the mouse, the selecteditem is set correctly but the autocompletebox doesn't get updated with the selecteditem.
That is, arrow down and select an item and the autocompletebox reflects the selected item whereas just hovering over and selecting the item means the autocompletebox does not reflect the selection - instead it shows what the user typed into the box; note that the backing property is aware of the selected item so it does work either way, only it's not reflected in the textbox.
Any help is appreciated.
Thanks
I should add that it is OnSelectedItemChanged that doesn't seem to get called...
This issue is explained here: http://www.siimviikman.com/2012/05/30/wpf-autocompleteboxfiltering-similar-items/
As precised in the end of the article, the user cannot navigate through items (with arrow keys + hit TAB). That is why I could not use their solution.
In the WPF Toolkit source code (UpdateTextCompletion method), one can read:
// Perform an exact string lookup for the text. This is a
// design change from the original Toolkit release when the
// IsTextCompletionEnabled property behaved just like the
// WPF ComboBox's IsTextSearchEnabled property.
//
// This change provides the behavior that most people expect
// to find: a lookup for the value is always performed.
newSelectedItem = TryGetMatch(text, _view, AutoCompleteSearch.GetFilter(AutoCompleteFilterMode.EqualsCaseSensitive));
So I simply patched the WPF Toolkit and commented out the calls to UpdateTextCompletion method, both in OnAdapterSelectionComplete and OnAdapterSelectionCanceled.
As I don't use text completion, this fix seems to work fine.

WPF: Handling bubbling events only from one specific content

Now I'm about to learn RoutedEvents. I understand bubbling and tunneling and all. But I have a little problem I'm not sure how to handle.
I've got a ItemsControl in my own defined UserControl ( inherits TabItem ). This itemsControl could be a ListBox or some selector not decided yet ( depends what is best with this solution ).
Now when one item there is selected ( Not sure yet what the best selection method would be, perhaps doubleclick ) I need to capture the event in the TabControl that holds my UserControl. Thankfully as my userControl is put into the XAML visualtree the bubbling event flows up and events can reach the TabControl. However I still have a few problems with implementing this.
1) Let's say I set it as MouseDoubleClick I don't wan't other DoubleClicks ( like in other tabs or outside the ItemsControlItems ). I guess you can always do a check where the events coming from to ensure it's the right one but I don't feel it's the right way and it could be asking for trouble.
2) If I set the event to SelectionChanged and the ItemsControl to ListBox I would have to ensure the ListBox itself doesn't handle it so it can reach the tabControl. How would I do that?
3) Same as 2) but the TabControl is a selector itself so it would raise it's own selectionchange events wouldn't it. how would that work. Would the handler grab both from the listbox and the TabControl?
4) Not sure what ItemsControl I should use. If i'm only using it to doubleclick the item and then handle the event wouldn't ListBox be too much. It has many effects that would be undisireable like selection and such. Is it better to define your own ItemsControl or maybe use ListBox and overwrite some of it's properties like selection ( how would one do that? )?
5) The source of the event would be the ItemsContainer but I wan't the item itself. There is a easy way to get access to it isn't there?
6) Let's say I put a button and in the DataTemplate I wan't to attach the ID of the object through the button and capture it where the button click is handled to identify what item it was. Is there a property do do that or would I have to define it myself?
7) Would the best course of action here maybe to create my own event?
Your take on this issue and comments on what process is best to take here is appriciated.
EDIT: Decided to add little bit more info as asked.
What I'm aiming for; when I start the program I have a TabControl with 1 tab in it. This tab is different from the rest of the tabs will be because it holds a list of objects (CompanyModel). This list is the ItemsControl I mention. When you press one company from this list a new tab is added with info about that company.
My solution was to create a TabItem collection as a source for the TabControl. Then I would define my 2 types of TabItems in a seperate XAML source file (not sure what to call it, basicly add new usercontrol). One is the initial where thhe list appears, the other one takes in a CompanyModel as a constructor parameter and shows info about that.
This works while I don't like the idea of defining the company list tabitem seperatly I can't define it under the tabcontrol because I can only have defined items or sourced, not both.
I've tried this idea by putting a button on the List DataTemplate that bubbles the Id of the company selected up to the TabControl, handling it there and adding a new TabItem with the CompanyModel. That was just a solution to try it and has problems like if I add anpther button to the CompanyTabs I would grab them aswell in the TabControl.
So my problem could be solved by finding a good way to bubble a event from the original listTab or If I would be able to define the tab in the same place as the TabControl.
I know that one other solution would to have the list seperate from the TabControl but others would like to see how this comes out and I would like to learn how I would do this.
please have a look in this blog, may be it will helps u.
http://blogs.u2u.be/diederik/post/2009/09/09/In-WPF-SelectionChanged-does-not-mean-that-the-selection-changed.aspx

Is it possible to get notification while adding items into itemsControl using Add() method in WPF

Actually I'm using an items control and animating the children of it. The problem is when adding the items dynamically through Add() method the first item is not getting animated and just comes to display as usual.
I've planned to hide the dynamically added child and later make it visible when i want to use it with animation and that way i can bring it into view with animation.
To hide the child when it gets added dynamically i need some kinda of notification or event fired for Add() method!. I tried using InotifyCollectionChanged but that didn't work.
Your thoughts please..
Your alternative suggestions are also appreciated..
Thanks in advance...
First option is to check this question: Animate WPF Datatemplate when item added to Listbox?.
Another option would be to create your own ItemsControl and override OnItemsChanged() method. But it's not as easy as the first option.
Hope this helps.
I'd probably bind the listbox to a collection in a ViewModel, and add directly to that rather than adding to the ListBox.
Hi guys i found the solution for my problem, this may help those who are struck with such an issue.
This can be achieved by using a grid as your itemsHost and dynamically adding the children of itemscontrol to the grid and animating it. since the grid get its children only at the runtime we have more control over it and decide when it should be rendered on the screen.

WPF DataGrid ContextMenu(s)

How can I define different ContextMenus for each column in the DataGrid (Microsoft's grid)? I found out that the grid provides a ContextMenu attribute, but i want different context menu items for the columns, not the same ContextMenu for the whole grid.
Edit: Thanks for your answer! I tried to listen to the ContextMenuOpeningEvent as you suggested which was a first success: the ContextMenu can be modified in the EventHandler. But it raises another (hopefully small) problem - I now have to identify the column the mouse cursor was over when the ContextMenuOpeningEvent was triggered. I'm going to research how to do (or work around) that later.
I haven't played with it at all, so this might be wrong, but you may be able to override the ContextMenuOpening event and create the appropriate menu on the fly.
add menu item to default context menu might give you a starting point.
Good luck.

Resources