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

WPF – Remove focus when clicking outside of a textbox

Rather than adding new control to window, I think you should give your Grid a name and react to the MouseDown event on your window, moving the focus to the Grid itself. Something like this: <Window x:Class=”WpfApplication1.Window1″ xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation” xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml” Title=”Window1″ Height=”412″ Width=”569″ MouseDown=”Window_MouseDown” Name=”window1″> <Grid ShowGridLines=”False” Background=”#01FFFFFF” KeyDown=”Grid_KeyDown” Name=”grid1″ Focusable=”True”> <TextBox Width=”120″ Margin=”117,61,0,0″ Name=”textBox1″ VerticalAlignment=”Top” … 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

Sort a wpf datagrid programmatically

voo’s solution was not working for me, ItemsSource was null, most likely because it was not directly set, but bound. All other solutions I found here at StackOverflow were dealing with sorting the Model only, but the DataGrid header was not reflecting to the sort. Here’s a proper solution based on the incomplete script here: … 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 you pass parameters from xaml?

Your constructor: public ClockControl(String city) { InitializeComponent(); this.initController(); //… } First of all, if you want to use ClockControl from XAML, then you need a default constructor, means a constructor which doesn’t take any parameter. So the above constructor is not going to work. I would suggest you to define a property with name City, … Read more

WPF – converting Bitmap to ImageSource

For others, this works: //If you get ‘dllimport unknown’-, then add ‘using System.Runtime.InteropServices;’ [DllImport(“gdi32.dll”, EntryPoint = “DeleteObject”)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool DeleteObject([In] IntPtr hObject); public ImageSource ImageSourceFromBitmap(Bitmap bmp) { var handle = bmp.GetHbitmap(); try { return Imaging.CreateBitmapSourceFromHBitmap(handle, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); } finally { DeleteObject(handle); } }

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