When to focus first element in ItemsControl after data-binding in WPF - c#

I have an ItemsControl control which is data-bound to a list. In the ItemsControl is a DataTemplate which displays all data-bound items as buttons.
Now I want the first button to receive focus.
When do set the focus? Doing this in the ContentRendered and DataContextChanged events don't work the controls don't seem to be rendered at that point.
Bonus question: what's the best way to look up such a button on my window?

I'm guessing the DataContextChanged event doesn't work because the DataContext actually changes before the ObservableCollection you are binding to has any content.
I haven't had a chance to test it, but just a thought, perhaps you could try setting the focus in the TargetUpdated event handler on your binding to your ItemsSource.
The only issue with this is that if the collection continues to update then the focus will continually return to the first button, but this is something you can handle with a simple flag.

Related

ListView LostFocus Event isn't working fine

I've a listview and a textBLOCK inside it. I want to collapse the visibility of listview when I tap anywhere else on my screen. I tried to do this using LostFocus Event for my listview but it is fired ONLY when I selected an item. Why does it behave like that?
Thanks in advance!
The only way to do this as of right now, based on this seems to be to hook into the touch input directly from the CoreWindow and then each time compare the point on the touch input with the bounds of the ListView relative to the CoreWindow.
The WinRTXamlToolkit may assist in this by adding such extensions as GetBoundingRect to FrameworkElements.
I used the IsTabStop Property. Actually i have a user control in in which i'm having this Listview. I did the following steps:
I set the IsTabStop property of UserControl to "True".
Then I set the focus to the textblock when it is tapped by doing:
this.Focus(FocusState.Pointer);
Then created the LostFocus Event of my UserControl and Collapsed the visibility of ListView.
Try using TextBlock's Leave or LostFocus event because if you focus the TextBlock, the ListView losts focus but if you focus TextBlock when you focused something out of ListView, the ListView never becomes focus.

Monitor ListView selection changes using a binding

I have a ListView of items, and I want to run some code every time the user selects or deselects an item without resorting to event handlers in the control's codebehind - everything is being done in the view the control has its datacontext set to.
When the ListView's selection mode is "Single" I can simply bind "SelectedItem" to a property in my view, and watch for when that property's change event. If selection mode is "Multiple" however, the behaviour is completely unreliable. Sometimes the last item clicked changes the SelectedItem, and sometimes it doesn't. This DependencyProperty appears to be complete trash when the selection mode isn't single. How else can I use a binding to track changes to the ListView's SelectedItems collection?
Note that I don't use Expression blend so I won't be using Interaction.Triggers or similar library solutions.
<ListView ItemsSource="{Binding Path=Zones}"
SelectionMode="Multiple"
SelectedItem="{Binding SelectedZone}">
Don't see any other way then described in this good article.
The thing is, that unfrotunately, SelectedItems property is readonly, so can not be databinded.
Till now it's kind of tricky story, unfortunately.
The only solution was to wrap the ItemsTemplate for the list in a control that can be toggled and has a command binding (like a button) and then bind the viewmodel to that command binding. It's a huge pain and requires hacking around with the HitTestVisibility and binding the button's state to the item's selected state, but it works in the end.

How to get the name of the collection an item is bound to?

Apologies for not posting any code for this but all my efforts seem to be going nowhere.
I need to get the name of an observableCollection a textblock is bound to from the mouseDown event handler, so I can then perform some operations on the data, is there any way to do this?
I have set the Tag of the textblock to {binding} so i get the entire object back the textblock is bound to. Other than this im not sure where to go next.
Update:
The reason for this is that I have 2 multiselect treeviews, with a heirarchial data template, each template shares the same treeview but is bound to a different observable collection.
The way my multiselect works is by applying a style to each treeview if the IsSelected value of that item in my collection is true.
Now I am using the Mousedown event handler for the textblock in my datatemplate to get the item I am working on, BUT some items can be in both collections at once. I need to know which item to set the IsSelected value on. Using Binding{tag} I get the Item I need to set on but not the collection it is within.
I'm using Mousedown as the event handler because If you click on one item to select it, then click again it needs to unselect and the treeview event handlers didnt seem to allow this to happen (SelectedItemChanged etc).
As a side note I also need to be able to hide the default selected style of the Treeview as this isnt used and it gets confusing.
You can't determine what collection an item is in, but you can determine what TreeView the user clicked on. Then you can get the collection by knowing the TreeView, which should solve your immediate problem.

WPF Listview/Gridview setting selected item when combobox is clicked

I'm having issues using a GridView as a ListView's View, what I want to do is fire an event when a user makes a selection from a combobox within the Gridview and pass the selected item within the event.
My first issue is that when the user clicks the combobox within a row, the row isnt selected (meaning the selecteditem stays null unless they click elsewhere first). Is there a clean way to do this without trying to catch mouse clicks or anything?
Secondly theres no selectionchangecommited event on a WPF combobox, is there a cleaner way to check if a user has manually selected an option other than checking if the combobox is enabled?
Thanks
I am seeing a similar behavior. My hypothesis is that one or more layers of the DataTemplate of each item in the list is swallowing the RoutedEvent that should have resulted in a new selection. Is it possible to, in a generic way, tell the items in the DataTemplate that they should never stop the event from bubbeling or tunneling further, without having to override every focus-triggering event handler in code behind?
I 'solved' my issue by using the WPF Toolkit grid (http://wpf.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=29117) and a series of selected items, which is probably a cleaner solution anyway.
Doesn't explain the behavior of the GridView, which is to me unusual
Original link has died, believe this to be the one https://github.com/xceedsoftware/wpftoolkit/wiki/DataGrid

Set different SelectionChanged Event Handler for a DataGrid inside another DataGrid

I have a DataGrid (WPF Toolkit) with a custom combobox like edit template of a cell. This custom combo box have another datagrid like popup.
I have this problem:
In the window constructor I assign the event handler to the master datagrid with this statement
this.dgDoc.SelectionChanged += new SelectionChangedEventHandler(dgDoc_SelectionChanged);
the problem is that the function dgDoc_SelectionChanged fire also when I change selection on datagrid of combobox popup.
How can I avoid this behavior?
the events are bubbling up the tree and are finding a handler. bummer.
why dont you just check who the sender is and if it is the inner grid, ignore them, if it is the grid you are interested in handle them

Categories