How to do InputBindings for a DataBound ListBox? - wpf

I have a ListBox with ItemsSource bound. If anyone selects a ListBoxItem and presses Enter key, a command should be executed. How could I do this? I have ItemContainerStyle for ListBox and I cannot find a way to set InputBindings through Style.
Any ideas?

You could create an attached helper property which sets the bindings, here would be an example, you then can set that property in the style.

Related

How to get current item index of ItemsControls in viewmodel?

How to get current item index of ItemsControls in viewmodel?
We want to delete the selected textbox item using ctrl+D in the itemscontrol from the viewmodel
As #metacircle has mentioned, you should use a ListBox, which has this functionality built in, instead of your ItemsControl. Using a ListBox, you have a number of options for accessing the selected item:
SelectedIndex Property
SelectedItem Property
SelectedValue Property
You can find code examples on these pages on MSDN and further examples in the ListBox Class page.
ItemsControl does not have the concept of "current item" nor "current index".
You're looking for a Selector-derived control, such as ListBox

Enable textbox from checkbox inside ItemsControl.ItemTemplate

I have an ItemsControl that I've bound to an IEnumerable. In the DataTemplate, I am creating a CheckBox for each Enum.
For ONE of the Enum's, I want to enable a textbox outside of the ItemsControl if it is checked. How can I do this?

How to achieve ItemSelection and IsMouseOver effect in ItemsControl WPF?

I am using ItemsControl in my application, applied ItemTemplate and ItemsPanelTemplate to it with a ScrollViewer style. All is fine. Binding is working pretty fine but here the issue: I want to select an item in ItemsControl but I am not able to do so as we do in ListBox using IsSelected property in triggers.
How can I achieve item selection and IsMouseOver in ItemsControl similar to ListBox, is there any way, as I did not find any resource on net useful enough so thought to ask for some help here.

How to set focus on element on ItemsControl?

I've ItemsControl that its ItemsSource property bind to some dictionary from code behind. The ItemTemplate is consist of only one button. So, for each item in dictionary it creates button.
My question is how can I set focus to one of these buttons (dynamically)?
Should I use ItemContainerGenerator.ContainerFromItem ?
Any other idea?
Thanks in advance!
Yes, use can ItemContainerGenerator.ContainerFromItem to get a container for your data item, then you will need to find you button inside this container and call Focus() on the button.
OR you can use an attached property to bind IsFocused to a property on your data item. See Set focus on textbox in WPF from view model (C#)

WPF - databinding label on listbox ismouseover

I'm still new to WPF, and I'm trying to do something that's beyond my knowledge at the moment.
I have a listbox databinded to the source collection, and a label. I'd like to bind the label's Content value to the listbox's item over which is mouse hovered.
Say I have DataTemplate binded to the class MenuItem:
<DataTemplate DataType="{x:Type local:MenuItem}" x:Key="MenuListTemplate">
Which has member Text. I want my Label to display Text from element which is mouse overed in list. I have the IsMouseOver trigger for my textbox, but have no idea how to bind Label.Content to it.
Any tips?
I don't think that binding can achieve your goal with ease. I think it's easier to do with routed events.
Subscribe to the MouseMove event at the ListBox level. Check if the source of the event is a ListBoxItem, and if it is use this item to update the label.

Resources