MVVM and the TextBox’s SelectedText property

There’s no straightforward way to bind SelectedText to a data source, because it’s not a DependencyProperty… however, it quite easy to create an attached property that you could bind instead. Here’s a basic implementation : public static class TextBoxHelper { public static string GetSelectedText(DependencyObject obj) { return (string)obj.GetValue(SelectedTextProperty); } public static void SetSelectedText(DependencyObject obj, string … Read more

ObservableCollection not updating View

You need to raise the change notification on the Level2MenuItems property. Instead of having public ObservableCollection<EMSMenuItem> Level2MenuItems { get; set; } you need private ObservableCollection<EMSMenuItem> _level2MenuItems; public ObservableCollection<EMSMenuItem> Level2MenuItems { get { return _level2MenuItems; } set { _level2MenuItems = value; RaisePropertyChanged(nameof(Level2MenuItems)); } } The reason the former works in the constructor is that the Binding … Read more

Can I somehow temporarily disable WPF data binding changes?

This extension of ObservableCollection solves the problem easily. It exposes a public SupressNotification property to allow the user to control when CollectionChanged notification will be suppressed. It does not offer range insertion/deletion, but if CollectionChanged notification is suppressed, the need to do range operation on the collection diminishes in most of the cases. This implementation … Read more

How do you pass a BitmapImage from a background thread to the UI thread in WPF?

The following uses the dispatcher to execute an Action delegate on the UI thread. This uses a synchronous model, the alternate Dispatcher.BeginInvoke will execute the delegate asynchronously. var backgroundThreadImage = GenerateImage(coordinate); GeneratedImage.Dispatcher.Invoke( DispatcherPriority.Normal, new Action(() => { GeneratedImage = backgroundThreadImage; })); UPDATE As discussed in the comments, the above alone will not work as the … Read more

Binding a WPF DataGridComboBoxColumn with MVVM

Here is a working code. The key point here was to use SelectedValueBinding instead of SelecteItemBinding. <DataGridComboBoxColumn Header=”Title” SelectedValueBinding=”{Binding TitleId}” SelectedValuePath=”TitleId” DisplayMemberPath=”TitleText” > <DataGridComboBoxColumn.ElementStyle> <Style TargetType=”ComboBox”> <Setter Property=”ItemsSource” Value=”{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.Titles}”/> </Style> </DataGridComboBoxColumn.ElementStyle> <DataGridComboBoxColumn.EditingElementStyle> <Style TargetType=”ComboBox”> <Setter Property=”ItemsSource” Value=”{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.Titles}”/> </Style> </DataGridComboBoxColumn.EditingElementStyle> </DataGridComboBoxColumn>

WPF: How to bind a command to the ListBoxItem using MVVM?

Unfortunately, only ButtonBase derived controls have the possibility for binding ICommand objects to their Command properties (for the Click event). However, you can use an API provided by Blend to map an event (like in your case MouseDoubleClick on the ListBox) to an ICommand object. <ListBox> <i:Interaction.Triggers> <i:EventTrigger EventName=”MouseDoubleClick”> <i:InvokeCommandAction Command=”{Binding YourCommand}”/> </i:EventTrigger> </i:Interaction.Triggers> </ListBox> … Read more

MVVM for winforms [duplicate]

I think that there are two answers here… really just one answer to “Should I” and one answer to “Could I”. As far as “Could I”, it is certainly possible. MVVM really just relies on a view that can bind to a view model. Since WinForms supports binding, this certainly is possible. You may need … Read more