two itemtemplates for one listbox

You can define DataTemplates that apply to any instance of a specific type by specifying the DataType without an x:Key. Using this method you don’t assign anything to ItemTemplate – the templates are applied automatically. <ListBox ItemsSource=”{Binding Path=MixedList}”> <ListBox.Resources> <DataTemplate DataType=”{x:Type local:BananaViewModel}”> <TextBlock Text=”{Binding Name}” Foreground=”Yellow”/> </DataTemplate> <DataTemplate DataType=”{x:Type local:AppleViewModel}”> <TextBlock Text=”{Binding Name}” Foreground=”Red”/> </DataTemplate> … Read more

How can I sort a ListBox using only XAML and no code-behind?

Use a CollectionViewSource: <CollectionViewSource x:Key=”SortedItems” Source=”{Binding CollectionOfStrings}” xmlns:scm=”clr-namespace:System.ComponentModel;assembly=Win‌​dowsBase”> <CollectionViewSource.SortDescriptions> <scm:SortDescription PropertyName=”SomePropertyOnYourItems”/> </CollectionViewSource.SortDescriptions> </CollectionViewSource> <ListBox ItemsSource=”{Binding Source={StaticResource SortedItems}}”/> You might want to wrap your strings in a custom VM class so you can more easily apply sorting behavior.

Child elements of scrollviewer preventing scrolling with mouse wheel?

You can also create a behavior and attach it to the parent control (in which the scroll events should bubble through). // Used on sub-controls of an expander to bubble the mouse wheel scroll event up public sealed class BubbleScrollEvent : Behavior<UIElement> { protected override void OnAttached() { base.OnAttached(); AssociatedObject.PreviewMouseWheel += AssociatedObject_PreviewMouseWheel; } protected override … Read more

Double Click a ListBox item to open a browser

You can add a style to ListBox.ItemContainerStyle, and add an EventSetter there: <ListBox> …. <ListBox.ItemContainerStyle> <Style TargetType=”{x:Type ListBoxItem}” BasedOn=”{StaticResource {x:Type ListBoxItem}}”> <EventSetter Event=”MouseDoubleClick” Handler=”ListBoxItem_MouseDoubleClick”/> </Style> </ListBox.ItemContainerStyle> </ListBox> ListBoxItem_MouseDoubleClick is a method in your code behind with the correct signature for MouseDoubleClick.

WPF Binding a ListBox to an enum, displaying the Description Attribute

Yes, it is possible. This will do it. Say we have the enum public enum MyEnum { [Description(“MyEnum1 Description”)] MyEnum1, [Description(“MyEnum2 Description”)] MyEnum2, [Description(“MyEnum3 Description”)] MyEnum3 } Then we can use the ObjectDataProvider as xmlns:MyEnumerations=”clr-namespace:MyEnumerations” <ObjectDataProvider MethodName=”GetValues” ObjectType=”{x:Type sys:Enum}” x:Key=”MyEnumValues”> <ObjectDataProvider.MethodParameters> <x:Type TypeName=”MyEnumerations:MyEnum” /> </ObjectDataProvider.MethodParameters> </ObjectDataProvider> And for the ListBox we set the ItemsSource to … Read more

ItemContainerGenerator.ContainerFromItem() returns null?

I found something that worked better for my case in this StackOverflow question: Get row in datagrid By putting in UpdateLayout and a ScrollIntoView calls before calling ContainerFromItem or ContainerFromIndex, you cause that part of the DataGrid to be realized which makes it possible for it return a value for ContainerFromItem/ContainerFromIndex: dataGrid.UpdateLayout(); dataGrid.ScrollIntoView(dataGrid.Items[index]); var row … Read more