WPF BackgroundWorker vs. Dispatcher

The main difference between the Dispatcher and other threading methods is that the Dispatcher is not actually multi-threaded. The Dispatcher governs the controls, which need a single thread to function properly; the BeginInvoke method of the Dispatcher queues events for later execution (depending on priority etc.), but still on the same thread. BackgroundWorker on the … Read more

How can I tell my DataTemplate to bind to a property in the PARENT ViewModel?

The answer is this: <DataTemplate x:Key=”CodeGenerationMenuTemplate”> <MenuItem Header=”{Binding Title}” Command=”{Binding DataContext.SwitchPageCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Menu}}}” CommandParameter=”{Binding Title}”/> </DataTemplate> I just saw that Nir had given me the syntax to solve the above issue on this question: What is the best way in MVVM to build a menu that displays various pages?.

CommandManager.InvalidateRequerySuggested() isn’t fast enough. What can I do?

CommandManager.InvalidateRequerySuggested() tries to validate all commands, which is totally ineffective (and in your case slow) – on every change, you are asking every command to recheck its CanExecute()! You’d need the command to know on which objects and properties is its CanExecute dependent, and suggest requery only when they change. That way, if you change … Read more

Setting the Visibility of an Element to Collapsed when Storyboard completes using XAML

you can do this in the animation as well <Window.Resources> <Storyboard x:Key=”OnLoaded1″> <ObjectAnimationUsingKeyFrames BeginTime=”00:00:00″ Storyboard.TargetName=”button” Storyboard.TargetProperty=”(UIElement.Visibility)”> <DiscreteObjectKeyFrame KeyTime=”00:00:00″ Value=”{x:Static Visibility.Visible}”/> <DiscreteObjectKeyFrame KeyTime=”00:00:00.8000000″ Value=”{x:Static Visibility.Collapsed}”/> <DiscreteObjectKeyFrame KeyTime=”00:00:01.4000000″ Value=”{x:Static Visibility.Visible}”/> </ObjectAnimationUsingKeyFrames> </Storyboard> </Window.Resources> <Window.Triggers> <EventTrigger RoutedEvent=”FrameworkElement.Loaded”> <BeginStoryboard Storyboard=”{StaticResource OnLoaded1}”/> </EventTrigger> </Window.Triggers>

How do I dynamically generate columns in a WPF DataGrid?

Ultimately I needed to do two things: Generate the columns manually from the list of properties returned by the query Set up a DataBinding object After that the built-in data binding kicked in and worked fine and didn’t seem to have any issue getting the property values out of the ExpandoObject. <DataGrid AutoGenerateColumns=”False” ItemsSource=”{Binding Results}” … Read more

Recommendations on a WPF Docking Library

The one from Codeproject is the AvalonDock – we use it for more then half a year now, but we’re far from release yet so we have the flexibility. Before ending up with AvalonDock we tried Infragistix, ActiPro, SandDock and may be some others. Even though AvalonDock is not 100% bug free (well what is?) … 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.

tech