Data binding to SelectedItem in a WPF Treeview

I realise this has already had an answer accepted, but I put this together to solve the problem. It uses a similar idea to Delta’s solution, but without the need to subclass the TreeView: public class BindableSelectedItemBehavior : Behavior<TreeView> { #region SelectedItem Property public object SelectedItem { get { return (object)GetValue(SelectedItemProperty); } set { SetValue(SelectedItemProperty, … Read more

How to automatically select all text on focus in WPF TextBox?

We have it so the first click selects all, and another click goes to cursor (our application is designed for use on tablets with pens). You might find it useful. public class ClickSelectTextBox : TextBox { public ClickSelectTextBox() { AddHandler(PreviewMouseLeftButtonDownEvent, new MouseButtonEventHandler(SelectivelyIgnoreMouseButton), true); AddHandler(GotKeyboardFocusEvent, new RoutedEventHandler(SelectAllText), true); AddHandler(MouseDoubleClickEvent, new RoutedEventHandler(SelectAllText), true); } private static void … Read more

Difference between ObservableCollection and BindingList

An ObservableCollection can be updated from UI exactly like any collection. The true difference is rather straightforward: ObservableCollection<T> implements INotifyCollectionChanged which provides notification when the collection is changed (you guessed ^^) It allows the binding engine to update the UI when the ObservableCollection is updated. However, BindingList<T> implements IBindingList. IBindingList provides notification on collection changes, … Read more

How should the ViewModel close the form?

I was inspired by Thejuan’s answer to write a simpler attached property. No styles, no triggers; instead, you can just do this: <Window … xmlns:xc=”clr-namespace:ExCastle.Wpf” xc:DialogCloser.DialogResult=”{Binding DialogResult}”> This is almost as clean as if the WPF team had gotten it right and made DialogResult a dependency property in the first place. Just put a bool? … Read more

Databinding an enum property to a ComboBox in WPF

You can create a custom markup extension. Example of usage: enum Status { [Description(“Available.”)] Available, [Description(“Not here right now.”)] Away, [Description(“I don’t have time right now.”)] Busy } At the top of your XAML: xmlns:my=”clr-namespace:namespace_to_enumeration_extension_class and then… <ComboBox ItemsSource=”{Binding Source={my:Enumeration {x:Type my:Status}}}” DisplayMemberPath=”Description” SelectedValue=”{Binding CurrentStatus}” SelectedValuePath=”Value” /> And the implementation… public class EnumerationExtension : MarkupExtension … Read more

Globally catch exceptions in a WPF application?

Use the Application.DispatcherUnhandledException Event. See this question for a summary (see Drew Noakes’ answer). Be aware that there’ll be still exceptions which preclude a successful resuming of your application, like after a stack overflow, exhausted memory, or lost network connectivity while you’re trying to save to the database.

Page vs Window in WPF?

Pages are intended for use in Navigation applications (usually with Back and Forward buttons, e.g. Internet Explorer). Pages must be hosted in a NavigationWindow or a Frame Windows are just normal WPF application Windows, but can host Pages via a Frame container

How to bind to a PasswordBox in MVVM

Maybe I am missing something, but it seems like most of these solutions overcomplicate things and do away with secure practices. This method does not violate the MVVM pattern and maintains complete security. Yes, technically it is code behind, but it is nothing more than a “special case” binding. The ViewModel still has no knowledge … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)