How to set Control Template in code?

Creating template in codebehind is not a good idea, in theory one would do this by defining the ControlTemplate.VisualTree which is a FrameworkElementFactory. ControlTemplate template = new ControlTemplate(typeof(Button)); var image = new FrameworkElementFactory(typeof(Image)); template.VisualTree = image; Assigning properties is very roundabout since you need to use SetValue and SetBinding: image.SetValue(Image.SourceProperty, …); Also, about the (previously) … Read more

Moving to next control on Enter keypress in WPF

Below is an attached property that I’ve used for just this. First, example usage: <TextBox Width=”100″ Text=”{Binding Name, Mode=TwoWay}” UI:FocusAdvancement.AdvancesByEnterKey=”True” /> (UI is the namespace alias for where I’ve defined the following.) The attached property: public static class FocusAdvancement { public static bool GetAdvancesByEnterKey(DependencyObject obj) { return (bool)obj.GetValue(AdvancesByEnterKeyProperty); } public static void SetAdvancesByEnterKey(DependencyObject obj, bool … Read more

Get the item doubleclick event of listview

<ListView.ItemContainerStyle> <Style TargetType=”ListViewItem”> <EventSetter Event=”MouseDoubleClick” Handler=”listViewItem_MouseDoubleClick” /> </Style> </ListView.ItemContainerStyle> The only difficulty then is if you are interested in the underlying object the listviewitem maps to e.g. private void listViewItem_MouseDoubleClick(object sender, MouseButtonEventArgs e) { ListViewItem item = sender as ListViewItem; object obj = item.Content; }

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.

Mutually exclusive checkable menu items?

This may not be what you’re looking for, but you could write an extension for the MenuItem class that allows you to use something like the GroupName property of the RadioButton class. I slightly modified this handy example for similarly extending ToggleButton controls and reworked it a little for your situation and came up with … Read more