Force redraw before long running operations

The following code will do what you’re looking for. However I would not use it. Use the BackgroundWorker class for long time operations. It’s easy to use and very stable. Here the code: public static void ProcessUITasks() { DispatcherFrame frame = new DispatcherFrame(); Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(delegate(object parameter) { frame.Continue = false; return null; }), null); … Read more

typesafe NotifyPropertyChanged using linq expressions

What does the code that raises this look like? I’m guessing it is something like: NotifyOfPropertyChange(() => SomeVal); which is implicitly: NotifyOfPropertyChange(() => this.SomeVal); which does a capture of this, and pretty-much means that the expression tree must be constructed (with Expression.Constant) from scratch each time. And then you parse it each time. So the … Read more

Binding a ComboBox to an enum nested in a class

Another way of getting the enum values for use as a data source: <Window.Resources> <ObjectDataProvider MethodName=”GetValues” ObjectType=”{x:Type sys:Enum}” x:Key=”TestValues”> <ObjectDataProvider.MethodParameters> <w:Type2 TypeName=”w:Test+TestEnum” /> </ObjectDataProvider.MethodParameters> </ObjectDataProvider> </Window.Resources> … ItemsSource=”{Binding Source={StaticResource TestValues}}” Note that you still need the Type2Extension because of weirdness with TypeExtension and nested types. But you wouldn’t need the extra custom markup extension. This … Read more

Bind a property to DataTemplateSelector

As evident from the error you can only bind with dependency property. But since it’s already inheriting from DataTemplateSelector, you cannot inherit from DependencyObject class. So, I would suggest to create an Attached property for binding purpose. But catch is attached property can only be applied on class deriving from DependencyObject. So, you need to … Read more

Why isn’t TextBox.Text in WPF animatable?

Trying to animate the TextBox manually …. var timeline = new StringAnimationUsingKeyFrames(); timeline.KeyFrames.Add(new DiscreteStringKeyFrame(“Goodbye”, KeyTime.FromTimeSpan(new TimeSpan(0,0,1)))); textControl.BeginAnimation(TextBox.TextProperty, timeline); …reveals a more useful error message. The last line fails with the following ArgumentException: ‘Text’ property is not animatable on ‘System.Windows.Controls.TextBox’ class because the IsAnimationProhibited flag has been set on the UIPropertyMetadata used to associate the property … Read more

How to programmatically navigate WPF UI element tab stops?

You do that using MoveFocus as shown in this MSDN article which explains everything about focus: Focus Overview. Here is some sample code to get to the next focused element (got it from that article, slightly modified). // MoveFocus takes a TraversalRequest as its argument. TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Next); // Gets the element with … Read more

WPF modeless dialog from MS Excel add-in

Solved it, courtesy of this link: Running WPF Application with Multiple UI Threads var thread = new Thread(() => { var wpfWindow = new WPFWindow(); wpfWindow.Show(); wpfWindow.Closed += (sender2, e2) => wpfWindow.Dispatcher.InvokeShutdown(); Dispatcher.Run(); }); thread.SetApartmentState(ApartmentState.STA); thread.Start();

Binding DynamicObject to a DataGrid with automatic column generation?

There is no uniform way to query dynamic properties, generally it’s expected that you know them ahead of time. With DynamicObject, implementers may override GetMemberNames and that generally gives you the properties, however it is really meant for debugging because there is no requirement that it provide all properties. Otherwise if it’s your own DynamicObject … Read more

MarkupExtension that uses a DataBinding value

Nevermind, I finally found out how the referenced code works and could come up with a solution. Here’s just a short explanation for the record. <TextBlock Text=”{t:Translate ‘import files’, {Binding FileCount}}”/> This requires a class TranslateExtension, inherited from MarkupExtension, with a constructor accepting two parameters, one String and one Binding. Store both values in the … Read more