Displaying html from string in WPF WebBrowser control

The WebBrowser has a NavigateToString method that you can use to navigate to HTML content. If you want to be able to bind to it, you can create an attached property that can just call the method when the value changes: public static class BrowserBehavior { public static readonly DependencyProperty HtmlProperty = DependencyProperty.RegisterAttached( “Html”, typeof(string), … Read more

Change Data template dynamically

A DataTemplateSelector does not respond to PropertyChange notifications, so it doesn’t get re-evaluated when your properties change. The alternative I use is DataTriggers that changes the Template based on a property. For example, this will draw all TaskModel objects using a ContentControl, and the ContentControl.Template is based on the TaskStatus property of the TaskModel <DataTemplate … Read more

Adding controls dynamically in WPF MVVM

If you really want to do mvvm , try to forget “how can I add controls”. You don’t have to, just think about your viewmodels – WPF create the contols for you 🙂 In your case lets say we have a SearchViewModel and a SearchEntryViewmodel. public class SearchEntryViewmodel { //Properties for Binding to Combobox and … Read more

How to format number of decimal places in wpf using style/template?

You should use the StringFormat on the Binding. You can use either standard string formats, or custom string formats: <TextBox Text=”{Binding Value, StringFormat=N2}” /> <TextBox Text=”{Binding Value, StringFormat={}{0:#,#.00}}” /> Note that the StringFormat only works when the target property is of type string. If you are trying to set something like a Content property (typeof(object)), … Read more

How do I use an icon that is a resource in WPF?

Your icon file should be added to one of your project assemblies and its Build Action should be set to Resource. After adding a reference to the assembly, you can create a NotifyIcon like this: System.Windows.Forms.NotifyIcon icon = new System.Windows.Forms.NotifyIcon(); Stream iconStream = Application.GetResourceStream( new Uri( “pack://application:,,,/YourReferencedAssembly;component/YourPossibleSubFolder/YourResourceFile.ico” )).Stream; icon.Icon = new System.Drawing.Icon( iconStream );

How do I convert a WPF size to physical pixels?

Transforming a known size to device pixels If your visual element is already attached to a PresentationSource (for example, it is part of a window that is visible on screen), the transform is found this way: var source = PresentationSource.FromVisual(element); Matrix transformToDevice = source.CompositionTarget.TransformToDevice; If not, use HwndSource to create a temporary hWnd: Matrix transformToDevice; … Read more