WPF: Cancel a user selection in a databound ListBox?

For future stumblers on this question, this page is what ultimately worked for me: http://blog.alner.net/archive/2010/04/25/cancelling-selection-change-in-a-bound-wpf-combo-box.aspx It’s for a combobox, but works for a listbox just fine, since in MVVM you don’t really care what type of control is calling the setter. The glorious secret, as the author mentions, is to actually change the underlying value … Read more

How to make WPF input control show virtual Keyboard when it got focus in touch screen

Try this, First check for a physical keyboard presence: KeyboardCapabilities keyboardCapabilities = new Windows.Devices.Input.KeyboardCapabilities(); return keyboardCapabilities.KeyboardPresent != 0 ? true : false; If you do not find a physical keyboard, use the in-built virtual keyboard of windows: Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.System) + Path.DirectorySeparatorChar + “osk.exe”); Got help from here: link 1 link 2

How to display items in Canvas through Binding

Set the ItemsPanel to a Canvas and bind the containers instead of the TextBlock in the DataTemplate <ItemsControl ItemsSource=”{Binding Path=ItemsToShowInCanvas}”> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <Canvas/> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> <ItemsControl.ItemContainerStyle> <Style TargetType=”ContentPresenter”> <Setter Property=”Canvas.Left” Value=”{Binding Left}”/> <Setter Property=”Canvas.Top” Value=”{Binding Top}”/> </Style> </ItemsControl.ItemContainerStyle> <ItemsControl.ItemTemplate> <DataTemplate> <TextBlock Text=”{Binding Path=Text}” /> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl>

Conditional DataTemplate

Don’t set the ItemTemplate but use an ItemTemplateSelector. DataTriggers would be fine too of course, spares you the extra class for the selector. e.g. <ItemsControl.ItemTemplate> <DataTemplate> <ContentControl Content=”{Binding}”> <ContentControl.Style> <Style TargetType=”ContentControl”> <Style.Triggers> <DataTrigger Binding=”{Binding ThatProperty}” Value=”1″> <Setter Property=”ContentTemplate” Value=”{StaticResource myDataTemplate1}” /> </DataTrigger> <DataTrigger Binding=”{Binding ThatProperty}” Value=”2″> <Setter Property=”ContentTemplate” Value=”{StaticResource myDataTemplate2}” /> </DataTrigger> </Style.Triggers> </Style> </ContentControl.Style> … Read more