Selected List view item not taking focus Win 10 UWP - c#

I bound Selected Item of ListView with my viewmodel property SelectedLayout and I have changed SelectedLayout from code behind. The Selected Item is changing properly But focus is not coming to the selected item. It is clear when I press enter. Focus is at button where it was before setting SelectedLayout . How can I get Focus to the selected Item in windows 10 UWP?

Just changing the value of bound property does not automatically change the focus to the corresponding element. It is done by design, as in many cases you do not need to change focus, just to update the value of the control. So what you need to do is to implement the logic of changing focus in your MVVM code.
To do that, the good practice would be to use Attached Property as outlined here: Set focus on textbox in WPF from view model (C#). In this manner, you can bind the IsFocused attached property of your controls to your ViewModel, and then implement any focusing logic in the ViewModel.

Related

Bind button enabled property to combobox selected item

A while back i had read a tutorial on data binding (maybe it was MVVM?) in Windows Forms. I've sense forgot everything and forgot the name of the tutorial.
What i would like to do is bind the enabled property of a button to a combox's selected item.
Logic: If combobox has selected item- enable button.
else disable button.
I'm aware of the combobox_textchanged and combobox_selecteditemchanged event and i would like to avoid using it if possible.
In WPF / MVVM, this is a UI issue that can be handled in a ViewModel class. In Windows Forms, you might also want to create a ViewModel class separate from your model class to keep UI concerns away from the Model classes. Either way, you can create a Boolean property like "IsActiveCustomer", or whatever it is in your case, in the object class you are binding to. Your property could either have a getter that returns a value based on the property that is bound to the combo box -- or you could use the combo box selected index changed or selected value changed events and set the Boolean property accordingly. Then, of course, bind the Enabled property of the button to the Boolean property. Probably need to know what you are data binding to in order to provide specifics (binding to object vs. BindingSource/table adapter, etc.)

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

WPF: DataBinding a ListBox where each item is a Tab Stop

I just built a WPF form that contains a ListBox. The ListBox has bound to it a list of TextBox controls. I need to make each TextBox control a TabStop so that a user can hit tab, type in a number, hit tab again and type in the next number, etc.
Problem is, the ListBox itself catches the tab then the next tab skips to following control after the ListBox.
Is there a way to make each TextBox inside the ListBox be tabbable (or perhaps another type of databound control that would work)?
Thanks
Well we don't really have enough information to answer the question (this depends on what Templates and Style the ListBox is using) but you'll potentially need to play with the KeyboardNavigation.TabNavigation property to change how to cycle through the items and set IsTabStop on the ListBox to false.
Something like:
<ListBox DataSource={Binding} IsTabStop="False" KeyboardNavigation.TabNavigation="Cycle" />

Categories