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.
Related
I'd like to create a CustomControl in WPF to present one to many items to the user in a certain manner.
For that I created a CustomControl with a dependency property ItemsSource and a dependency property ItemsTemplate. But I'm not sure how to join the business objects from the ItemsSource with the DataTemplate or ControlTemplate so that I have something WPFy to measure in the MeasureOverride and to place in the ArrangeOverride.
Do I use something like a ContentControl for each item and put my business object as DataContext?
Thank you for any advice or any push in the right direction.
After lots of googling and spying on the ItemsControl of WPF I think I have to use a ContentPresenter for each item which isn't a DependencyObject itself.
I have a scenario where in I populate a listbox with a 1000 items. I set the ItemsSource property with a source of data.
I have a requirement where I need to strike out an item of the listbox based on certain criteria, when the UI loads. I am using styles + attached properties to achieve the same by setting ContentTemplate of ListBoxItem in the callback method of attached property.
My problem is when I try to generate a ListBoxItem using ItemContainerGenerator.ContainerFromItem, for an item which is at the end of the list, I get null. As a result, I cannot strike out items of the listbox which are at the bottom of the list.
Has it got something to do with virtualization. I want to obtain all those ListBoxItems on load.
Is there any workaround?
Thanks
This is definitely caused by virtualization. This is exactly what UI virtualization is supposed to do - only create ListBoxItem objects for items that are visible on the screen. You can easily see that this is indeed the cause by setting VirtualizingStackPanel.IsVirtualizing = false on your ListBox and see that ItemContainerGenerator.ContainerFromItem no longer returns null.
You can set a style for your ListBoxItems in your ListBox that will have the logic to strike out the items as needed. This should also work when virtualization is enabled. For example:
<ListBox>
<ListBox.Resources>
<Style TargetType=ListBoxItem>
...
</Style>
</ListBox.Resources>
</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.
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.
I'm writing a generic control template for my WPF Custom Control.
But with ItemsPresenter I only got raw list of Data..
Compared to the ListBox, the ListBox has all features I need.
Is it wrong to use a ListBox instead of ItemsPresenter ?
What I'm after is that
if I write a generic Template that uses a ListBox and in code behind I register some ListBox specific events and somebody overrides my generic Template with his own ControlTemplate witn an ItemsControl inside that does not possess that event, it will raise an Exception. In case of ItemsPresenter, everyone could use what he wants to.
Thanks.
I think you could add some test to see if the ItemsControl in the template is a ListBox or not. For example:
var itemsControl = this.Template.FindName("PART_Items", this);
if(itemsControl is ListBox)
{
// wire additional event handler here
}