How to set focus on element on ItemsControl? - wpf

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#)

Related

How to do InputBindings for a DataBound ListBox?

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.

binding checkbox in datatemplate to user control datacontext

I have a listbox which include a data template of checkbox. I need the check box data context be the user control and not the listbox datacontext. How can active that in silverlight since donot have relativesource than self or template
Thanks in advance
Silverlight does not have relative source binding "out of the box", however there is a solution here that works:
http://www.scottlogic.co.uk/blog/colin/2009/02/relativesource-binding-in-silverlight/
You can bind your CheckBox's DataContext to that of the parent User Conntrol (In this example it is called MyUserControl) as follows:
<CheckBox>
<local:BindingHelper.Binding>
<local:BindingProperties TargetProperty="DataContext"
SourceProperty="DataContext"
RelativeSourceAncestorType="MyUserCOntrol"/>
</local:BindingHelper.Binding>
</CheckBox>
If you do not want to go down this route, you could create a ViewModel that exposes a collection of objects that you bind to your checkbox, each object could provide a reference back to the parent view model.

Silverlight Listbox selectable ItemTemplate

I have a Usercontrol that contains a ListBox (lstClients) and a ComboBox
The ListBox has 2 DataTemplates setup as Resources called "LowDetailTemplate" and "HighDetailTemplate"
I need to be able to switch between the 2 DataTemplates when I change the value in a ComboBox from "Low" to "High" and vice versa. In the SelectionChanged event of the ComboBox I'm guessing I need to change the ItemTemplate of the ListBox but I'm struggling with the code to assign the DataTemplate in code behind. My latest attempt is shown below but it fails at runtime.
lstClients.ItemTemplate = (DataTemplate)this.Resources["LowDetailTemplate"];
It would be easier to define both of the views inside the same data template and then switch which is visible by use of a simple variable. Then your combo box change would just update that simple variable and so cause the view shown for each template instance to change.

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.

How to get OnSelectionChanged ListBoxItem background change when modifying the data at the same time using a ListBox control?

I have a ListBox control with an ObservableCollection instance as the control's ItemsSource property.
Everything works fine, but when I handle the control's OnSelectionChanged, my business logic modifies the collection's data and I no longer get the ListBoxItem background change you usually get when your ListBox selection changes.
Did anyone encounter the same problem ? Any solution here ?
Thanks and best regards,
Romain
It sounds like you are losing your selected item reference when the list changes.
Bind your ListBox's SelectedItem to a property on your data context and handle selection changes there.

Resources