Table of Contents

Class XamlHelper

Namespace
Ecng.Xaml
Assembly
StockSharp.Xaml.dll

Miscellaneous WPF helper and extension methods for UI threading, bindings, imaging, dialogs, and type conversions.

public static class XamlHelper
Inheritance
XamlHelper
Inherited Members

Fields

DialogResultProperty

Attached property that wires up a Button to set its owning window DialogResult when clicked.

public static readonly DependencyProperty DialogResultProperty

Field Value

DependencyProperty

DisabledCommand

Disabled command returning false from CanExecute and throwing when executed.

public static readonly ICommand DisabledCommand

Field Value

ICommand

Properties

CurrentThreadDispatcher

Gets the Dispatcher associated with the current thread.

public static Dispatcher CurrentThreadDispatcher { get; }

Property Value

Dispatcher

Methods

CopyToClipboard<T>(T)

Copies the provided value to the system clipboard on the UI thread.

public static void CopyToClipboard<T>(this T value)

Parameters

value T

Type Parameters

T

DeleteHideable(Window)

Removes previously added hide-on-close behavior.

public static void DeleteHideable(this Window window)

Parameters

window Window

EnsureUIThread()

Throws if the current thread is not the UI thread.

public static void EnsureUIThread()

EnsureUIThread(object)

Ensures the current thread is the UI thread and throws otherwise.

public static void EnsureUIThread(this object obj)

Parameters

obj object

Any DispatcherObject or Dispatcher or other object to infer dispatcher from.

FindLogicalChild<T>(DependencyObject)

Searches the logical tree depth-first and returns the first child of the specified type, or null if none found.

public static T FindLogicalChild<T>(this DependencyObject obj) where T : DependencyObject

Parameters

obj DependencyObject

Returns

T

Type Parameters

T

FindLogicalParentOfType<T>(DependencyObject)

Walks up the logical tree and returns the first parent of the specified type, or null if none found.

public static T FindLogicalParentOfType<T>(this DependencyObject child) where T : DependencyObject

Parameters

child DependencyObject

Returns

T

Type Parameters

T

FindVisualChild<T>(DependencyObject)

Returns the first visual child of the specified type, or null if none found.

public static T FindVisualChild<T>(this DependencyObject obj) where T : DependencyObject

Parameters

obj DependencyObject

Returns

T

Type Parameters

T

FindVisualChilds<T>(DependencyObject)

Enumerates all visual children of the specified type.

public static IEnumerable<T> FindVisualChilds<T>(this DependencyObject obj) where T : DependencyObject

Parameters

obj DependencyObject

Returns

IEnumerable<T>

Type Parameters

T

FromWpf(HorizontalAlignment)

public static HorizontalAlignment FromWpf(this HorizontalAlignment value)

Parameters

value HorizontalAlignment

HorizontalAlignment value.

Returns

HorizontalAlignment

HorizontalAlignment value.

FromWpf(Brush)

Convert Brush value to DrawingBrush.

public static Brush FromWpf(this Brush value)

Parameters

value Brush

Brush value.

Returns

Brush

DrawingBrush value.

FromWpf(Color)

Convert Color value to Color.

public static Color FromWpf(this Color value)

Parameters

value Color

Color value.

Returns

Color

Color value.

FromWpf(Point)

Convert Point value to PointF.

public static PointF FromWpf(this Point value)

Parameters

value Point

Point value.

Returns

PointF

PointF value.

FromWpf(Thickness)

Convert Thickness value to Thickness.

public static Thickness FromWpf(this Thickness value)

Parameters

value Thickness

Thickness value.

Returns

Thickness

Thickness value.

FromWpf(VerticalAlignment)

public static VerticalAlignment FromWpf(this VerticalAlignment value)

Parameters

value VerticalAlignment

VerticalAlignment value.

Returns

VerticalAlignment

VerticalAlignment value.

GetActiveOrMainWindow(Application)

Returns the active window or the main window if none is active.

public static Window GetActiveOrMainWindow(this Application app)

Parameters

app Application

Returns

Window

GetActiveWindow(Application)

Returns the first active window, or null if none found.

public static Window GetActiveWindow(this Application app)

Parameters

app Application

Returns

Window

GetActiveWindows(Application)

Returns all currently active windows in the application.

public static IEnumerable<Window> GetActiveWindows(this Application app)

Parameters

app Application

Returns

IEnumerable<Window>

GetActualSize(FrameworkElement)

Returns the element ActualWidth/ActualHeight as integers tuple.

public static (int width, int height) GetActualSize(this FrameworkElement elem)

Parameters

elem FrameworkElement

Returns

(int width, int height)

GetBrush(DrawingImage)

Gets the first brush found in the DrawingImage's Drawing.

public static Brush GetBrush(this DrawingImage source)

Parameters

source DrawingImage

Returns

Brush

GetDialogResult(DependencyObject)

Boilerplate attached property "bool? DialogResult" used to set Window.DialogResult from a Button.

public static bool? GetDialogResult(DependencyObject obj)

Parameters

obj DependencyObject

Returns

bool?

GetImage(string, SolidColorBrush, SolidColorBrush)

Gets a themed SVG/Vector icon recolored with the specified fill and stroke brushes.

public static ImageSource GetImage(this string iconKey, SolidColorBrush fill, SolidColorBrush stroke = null)

Parameters

iconKey string
fill SolidColorBrush
stroke SolidColorBrush

Returns

ImageSource

GetImage(FrameworkElement)

Renders a FrameworkElement into a bitmap using its current size.

public static BitmapSource GetImage(this FrameworkElement elem)

Parameters

elem FrameworkElement

Returns

BitmapSource

GetImage(Visual, int, int)

Renders a Visual into a bitmap with the given width and height.

public static BitmapSource GetImage(this Visual visual, int width, int height)

Parameters

visual Visual
width int
height int

Returns

BitmapSource

GetImage(Visual, (int width, int height))

Renders a Visual into a bitmap using the specified width and height.

public static BitmapSource GetImage(this Visual visual, (int width, int height) size)

Parameters

visual Visual
size (int width, int height)

Returns

BitmapSource

GetSystemFontFamilies()

Get system font families.

public static IEnumerable<string> GetSystemFontFamilies()

Returns

IEnumerable<string>

System font families.

GetWindow(DependencyObject)

Gets the owning window for the specified object.

public static Window GetWindow(this DependencyObject obj)

Parameters

obj DependencyObject

Returns

Window

GuiAsync(Dispatcher, Action)

Executes the action on the UI thread asynchronously with normal priority.

public static void GuiAsync(this Dispatcher dispatcher, Action action)

Parameters

dispatcher Dispatcher
action Action

GuiAsync(Dispatcher, Action, DispatcherPriority)

Executes the action on the UI thread asynchronously with the specified priority.

public static void GuiAsync(this Dispatcher dispatcher, Action action, DispatcherPriority priority)

Parameters

dispatcher Dispatcher
action Action
priority DispatcherPriority

GuiAsync(DispatcherObject, Action)

Executes the action on the UI thread asynchronously using the object's Dispatcher.

public static void GuiAsync(this DispatcherObject obj, Action action)

Parameters

obj DispatcherObject
action Action

GuiSync(Dispatcher, Action)

Executes the action synchronously on the Dispatcher with normal priority.

public static void GuiSync(this Dispatcher dispatcher, Action action)

Parameters

dispatcher Dispatcher
action Action

GuiSync(Dispatcher, Action, DispatcherPriority)

Executes the action synchronously on the Dispatcher with the specified priority.

public static void GuiSync(this Dispatcher dispatcher, Action action, DispatcherPriority priority)

Parameters

dispatcher Dispatcher
action Action
priority DispatcherPriority

GuiSync(DispatcherObject, Action)

Executes the action synchronously on the object's Dispatcher.

public static void GuiSync(this DispatcherObject obj, Action action)

Parameters

obj DispatcherObject
action Action

GuiSync<T>(Dispatcher, Func<T>)

Executes the function synchronously on the Dispatcher with normal priority and returns the result.

public static T GuiSync<T>(this Dispatcher dispatcher, Func<T> func)

Parameters

dispatcher Dispatcher
func Func<T>

Returns

T

Type Parameters

T

GuiSync<T>(Dispatcher, Func<T>, DispatcherPriority)

Executes the function synchronously on the Dispatcher with the specified priority and returns the result.

public static T GuiSync<T>(this Dispatcher dispatcher, Func<T> func, DispatcherPriority priority)

Parameters

dispatcher Dispatcher
func Func<T>
priority DispatcherPriority

Returns

T

Type Parameters

T

GuiSync<T>(DispatcherObject, Func<T>)

Executes the function synchronously on the object's Dispatcher and returns its result.

public static T GuiSync<T>(this DispatcherObject obj, Func<T> func)

Parameters

obj DispatcherObject
func Func<T>

Returns

T

Type Parameters

T

GuiThreadGetAsync(Action<CancellationToken>, CancellationToken)

Invokes the provided action on the UI thread if required.

public static Task GuiThreadGetAsync(Action<CancellationToken> action, CancellationToken token)

Parameters

action Action<CancellationToken>
token CancellationToken

Returns

Task

GuiThreadGetAsync(Func<CancellationToken, Task>, CancellationToken)

Invokes the provided async action on the UI thread if required.

public static Task GuiThreadGetAsync(Func<CancellationToken, Task> func, CancellationToken token)

Parameters

func Func<CancellationToken, Task>
token CancellationToken

Returns

Task

GuiThreadGetAsync<TResult>(Func<CancellationToken, Task<TResult>>, CancellationToken)

Invokes the provided async function on the UI thread if required and returns its result.

public static Task<TResult> GuiThreadGetAsync<TResult>(Func<CancellationToken, Task<TResult>> func, CancellationToken token)

Parameters

func Func<CancellationToken, Task<TResult>>
token CancellationToken

Returns

Task<TResult>

Type Parameters

TResult

GuiThreadGetAsync<TResult>(Func<CancellationToken, TResult>, CancellationToken)

Invokes the provided function on the UI thread if required and returns its result.

public static Task<TResult> GuiThreadGetAsync<TResult>(Func<CancellationToken, TResult> func, CancellationToken token)

Parameters

func Func<CancellationToken, TResult>
token CancellationToken

Returns

Task<TResult>

Type Parameters

TResult

IsDesignMode(DependencyObject)

Returns true if running in design mode.

public static bool IsDesignMode(this DependencyObject obj)

Parameters

obj DependencyObject

Returns

bool

IsFontSupported(string)

Determines if font is supported.

public static bool IsFontSupported(string familyName)

Parameters

familyName string

Font family name.

Returns

bool

True if supported; otherwise false.

IsUI()

Determines the current thread is associated with Dispatcher.

public static bool IsUI()

Returns

bool

Check result.

LoadViewFromUri(Control, string)

Loads a XAML view from the specified relative pack URI into the control.

public static void LoadViewFromUri(this Control control, string baseUri)

Parameters

control Control
baseUri string

MakeHideable(Window)

Hooks Closing event to hide the window instead of closing it.

public static void MakeHideable(this Window window)

Parameters

window Window

ReColor(SvgImage, Color?, Color?)

Change SVG image stroke/fill colors. Original is not changed.

public static SvgImage ReColor(this SvgImage orig, Color? stroke, Color? fill)

Parameters

orig SvgImage
stroke Color?
fill Color?

Returns

SvgImage

SaveImage(BitmapSource, Stream)

Saves the bitmap into the provided stream in PNG format.

public static void SaveImage(this BitmapSource image, Stream file)

Parameters

image BitmapSource
file Stream

SetBindings(DependencyObject, DependencyProperty, object, string, BindingMode, IValueConverter, object)

Applies a simple single binding to the specified dependency property.

public static void SetBindings(this DependencyObject obj, DependencyProperty property, object dataObject, string path, BindingMode mode = BindingMode.TwoWay, IValueConverter converter = null, object parameter = null)

Parameters

obj DependencyObject
property DependencyProperty
dataObject object
path string
mode BindingMode
converter IValueConverter
parameter object

SetBindings(DependencyObject, DependencyProperty, object, PropertyPath, BindingMode, IValueConverter, object)

Applies a simple single binding to the specified dependency property with a PropertyPath.

public static void SetBindings(this DependencyObject obj, DependencyProperty property, object dataObject, PropertyPath path, BindingMode mode = BindingMode.TwoWay, IValueConverter converter = null, object parameter = null)

Parameters

obj DependencyObject
property DependencyProperty
dataObject object
path PropertyPath
mode BindingMode
converter IValueConverter
parameter object

SetDialogResult(DependencyObject, bool?)

Sets the attached DialogResult value for a button.

public static void SetDialogResult(DependencyObject obj, bool? value)

Parameters

obj DependencyObject
value bool?

SetMultiBinding(DependencyObject, DependencyProperty, IMultiValueConverter, params Binding[])

Creates a MultiBinding with provided bindings and converter and applies it to the specified property.

public static void SetMultiBinding(this DependencyObject obj, DependencyProperty prop, IMultiValueConverter conv, params Binding[] bindings)

Parameters

obj DependencyObject
prop DependencyProperty
conv IMultiValueConverter
bindings Binding[]

ShowModal(CommonDialog, DependencyObject)

Shows the CommonDialog as modal using the owning object to resolve the owner window.

public static bool ShowModal(this CommonDialog dlg, DependencyObject obj)

Parameters

dlg CommonDialog
obj DependencyObject

Returns

bool

ShowModal(CommonDialog, Window)

Shows the CommonDialog as modal with the specified owner window.

public static bool ShowModal(this CommonDialog dlg, Window owner)

Parameters

dlg CommonDialog
owner Window

Returns

bool

ShowModal(Window)

Shows the window as modal using Application.Current.MainWindow as owner if available.

public static bool ShowModal(this Window wnd)

Parameters

wnd Window

Returns

bool

ShowModal(Window, DependencyObject)

Shows the window as modal using the owning object to resolve the owner window.

public static bool ShowModal(this Window wnd, DependencyObject obj)

Parameters

wnd Window
obj DependencyObject

Returns

bool

ShowModal(Window, Window)

Shows the window as modal with the specified owner.

public static bool ShowModal(this Window wnd, Window owner)

Parameters

wnd Window
owner Window

Returns

bool

ShowModel<TWindow>(object, DependencyObject)

Creates and shows a modal Window of type TWindow with DataContext set to the provided model.

public static bool ShowModel<TWindow>(this object model, DependencyObject owner) where TWindow : Window, new()

Parameters

model object
owner DependencyObject

Returns

bool

Type Parameters

TWindow

ToBitmapImage(Stream, BitmapCreateOptions, BitmapCacheOption, bool)

Creates a BitmapImage from a stream with optional init parameters.

public static BitmapImage ToBitmapImage(this Stream streamSource, BitmapCreateOptions options = BitmapCreateOptions.None, BitmapCacheOption cache = BitmapCacheOption.Default, bool freeze = true)

Parameters

streamSource Stream
options BitmapCreateOptions
cache BitmapCacheOption
freeze bool

Returns

BitmapImage

ToColor(int)

Converts ARGB int to Color.

public static Color ToColor(this int color)

Parameters

color int

Returns

Color

ToInt(Color)

Converts a Color to ARGB int.

public static int ToInt(this Color color)

Parameters

color Color

Returns

int

ToSvg(Stream)

Convert stream to SVG image.

public static ImageSource ToSvg(this Stream stream)

Parameters

stream Stream

Stream

Returns

ImageSource

SVG image.

ToTransparent(Color, byte)

Returns a copy of the color with the specified alpha channel.

public static Color ToTransparent(this Color color, byte alpha)

Parameters

color Color
alpha byte

Returns

Color

ToWpf(Brush)

Convert DrawingBrush value to Brush.

public static Brush ToWpf(this Brush value)

Parameters

value Brush

DrawingBrush value.

Returns

Brush

Brush value.

ToWpf(HorizontalAlignment)

public static HorizontalAlignment ToWpf(this HorizontalAlignment value)

Parameters

value HorizontalAlignment

HorizontalAlignment value.

Returns

HorizontalAlignment

HorizontalAlignment value.

ToWpf(Thickness)

Convert Thickness value to Thickness.

public static Thickness ToWpf(this Thickness value)

Parameters

value Thickness

Thickness value.

Returns

Thickness

Thickness value.

ToWpf(VerticalAlignment)

public static VerticalAlignment ToWpf(this VerticalAlignment value)

Parameters

value VerticalAlignment

VerticalAlignment value.

Returns

VerticalAlignment

VerticalAlignment value.

ToWpf(Color)

Convert Color value to Color.

public static Color ToWpf(this Color value)

Parameters

value Color

Color value.

Returns

Color

Color value.

ToWpf(PointF)

Convert PointF value to Point.

public static Point ToWpf(this PointF value)

Parameters

value PointF

PointF value.

Returns

Point

Point value.

Try to open the specified link. If it fails, copies the URL to clipboard and shows an error message.

public static void TryOpenLink(this string url, DependencyObject owner)

Parameters

url string

Link.

owner DependencyObject

UI thread owner.

TryOpenWithInitialDir(DXFileDialog, DependencyObject, string)

Opens a file dialog using last used directory (if available) and stores the selected directory on close.

public static bool TryOpenWithInitialDir(this DXFileDialog dlg, DependencyObject owner, string ctrlName)

Parameters

dlg DXFileDialog
owner DependencyObject
ctrlName string

Returns

bool

TryOpenWithInitialDir(DXFolderBrowserDialog, DependencyObject, string)

Opens a folder browser dialog using last used directory (if available) and stores the selected directory on close.

public static bool TryOpenWithInitialDir(this DXFolderBrowserDialog dlg, DependencyObject owner, string ctrlName)

Parameters

dlg DXFolderBrowserDialog
owner DependencyObject
ctrlName string

Returns

bool

UpdateBrush(DrawingImage, Brush)

Updates all brushes inside the drawing image to the specified brush.

public static void UpdateBrush(this DrawingImage source, Brush brush)

Parameters

source DrawingImage
brush Brush

UpdatePen(DrawingImage, Pen)

Updates all pens inside the drawing image to the specified pen.

public static void UpdatePen(this DrawingImage source, Pen pen)

Parameters

source DrawingImage
pen Pen

Url2Img(Uri)

Creates an ImageSource from a pack or http(s) URI. Supports SVG via DevExpress.

public static ImageSource Url2Img(this Uri uri)

Parameters

uri Uri

Returns

ImageSource

WpfCast<T>(object)

Cast value to specified type.

public static T WpfCast<T>(this object value)

Parameters

value object

Source value.

Returns

T

Casted value.

Type Parameters

T

Return type.