Class XamlHelper
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
DisabledCommand
Disabled command returning false from CanExecute and throwing when executed.
public static readonly ICommand DisabledCommand
Field Value
Properties
CurrentThreadDispatcher
Gets the Dispatcher associated with the current thread.
public static Dispatcher CurrentThreadDispatcher { get; }
Property Value
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
objectAny 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)
Convert HorizontalAlignment value to HorizontalAlignment.
public static HorizontalAlignment FromWpf(this HorizontalAlignment value)
Parameters
value
HorizontalAlignmentHorizontalAlignment value.
Returns
FromWpf(Brush)
Convert Brush value to DrawingBrush.
public static Brush FromWpf(this Brush value)
Parameters
Returns
- Brush
DrawingBrush value.
FromWpf(Color)
public static Color FromWpf(this Color value)
Parameters
Returns
FromWpf(Point)
public static PointF FromWpf(this Point value)
Parameters
Returns
FromWpf(Thickness)
public static Thickness FromWpf(this Thickness value)
Parameters
Returns
FromWpf(VerticalAlignment)
Convert VerticalAlignment value to VerticalAlignment.
public static VerticalAlignment FromWpf(this VerticalAlignment value)
Parameters
value
VerticalAlignmentVerticalAlignment value.
Returns
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
GetActiveWindow(Application)
Returns the first active window, or null if none found.
public static Window GetActiveWindow(this Application app)
Parameters
app
Application
Returns
GetActiveWindows(Application)
Returns all currently active windows in the application.
public static IEnumerable<Window> GetActiveWindows(this Application app)
Parameters
app
Application
Returns
GetActualSize(FrameworkElement)
Returns the element ActualWidth/ActualHeight as integers tuple.
public static (int width, int height) GetActualSize(this FrameworkElement elem)
Parameters
elem
FrameworkElement
Returns
GetBrush(DrawingImage)
Gets the first brush found in the DrawingImage's Drawing.
public static Brush GetBrush(this DrawingImage source)
Parameters
source
DrawingImage
Returns
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
stringfill
SolidColorBrushstroke
SolidColorBrush
Returns
GetImage(FrameworkElement)
Renders a FrameworkElement into a bitmap using its current size.
public static BitmapSource GetImage(this FrameworkElement elem)
Parameters
elem
FrameworkElement
Returns
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
Returns
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
Returns
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
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
Dispatcheraction
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
Dispatcheraction
Actionpriority
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
DispatcherObjectaction
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
Dispatcheraction
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
Dispatcheraction
Actionpriority
DispatcherPriority
GuiSync(DispatcherObject, Action)
Executes the action synchronously on the object's Dispatcher.
public static void GuiSync(this DispatcherObject obj, Action action)
Parameters
obj
DispatcherObjectaction
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
Dispatcherfunc
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
Dispatcherfunc
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
DispatcherObjectfunc
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
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
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
IsFontSupported(string)
Determines if font is supported.
public static bool IsFontSupported(string familyName)
Parameters
familyName
stringFont 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
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
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
BitmapSourcefile
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
DependencyObjectproperty
DependencyPropertydataObject
objectpath
stringmode
BindingModeconverter
IValueConverterparameter
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
DependencyObjectproperty
DependencyPropertydataObject
objectpath
PropertyPathmode
BindingModeconverter
IValueConverterparameter
object
SetDialogResult(DependencyObject, bool?)
Sets the attached DialogResult value for a button.
public static void SetDialogResult(DependencyObject obj, bool? value)
Parameters
obj
DependencyObjectvalue
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
DependencyObjectprop
DependencyPropertyconv
IMultiValueConverterbindings
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
CommonDialogobj
DependencyObject
Returns
ShowModal(CommonDialog, Window)
Shows the CommonDialog as modal with the specified owner window.
public static bool ShowModal(this CommonDialog dlg, Window owner)
Parameters
dlg
CommonDialogowner
Window
Returns
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
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
Windowobj
DependencyObject
Returns
ShowModal(Window, Window)
Shows the window as modal with the specified owner.
public static bool ShowModal(this Window wnd, Window owner)
Parameters
Returns
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
objectowner
DependencyObject
Returns
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
Streamoptions
BitmapCreateOptionscache
BitmapCacheOptionfreeze
bool
Returns
ToColor(int)
Converts ARGB int to Color.
public static Color ToColor(this int color)
Parameters
color
int
Returns
ToInt(Color)
Converts a Color to ARGB int.
public static int ToInt(this Color color)
Parameters
color
Color
Returns
ToSvg(Stream)
Convert stream to SVG image.
public static ImageSource ToSvg(this Stream stream)
Parameters
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
Returns
ToWpf(Brush)
Convert DrawingBrush value to Brush.
public static Brush ToWpf(this Brush value)
Parameters
value
BrushDrawingBrush value.
Returns
ToWpf(HorizontalAlignment)
Convert HorizontalAlignment value to HorizontalAlignment.
public static HorizontalAlignment ToWpf(this HorizontalAlignment value)
Parameters
value
HorizontalAlignmentHorizontalAlignment value.
Returns
ToWpf(Thickness)
public static Thickness ToWpf(this Thickness value)
Parameters
Returns
ToWpf(VerticalAlignment)
Convert VerticalAlignment value to VerticalAlignment.
public static VerticalAlignment ToWpf(this VerticalAlignment value)
Parameters
value
VerticalAlignmentVerticalAlignment value.
Returns
ToWpf(Color)
public static Color ToWpf(this Color value)
Parameters
Returns
ToWpf(PointF)
public static Point ToWpf(this PointF value)
Parameters
Returns
TryOpenLink(string, DependencyObject)
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
stringLink.
owner
DependencyObjectUI 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
DXFileDialogowner
DependencyObjectctrlName
string
Returns
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
DXFolderBrowserDialogowner
DependencyObjectctrlName
string
Returns
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
DrawingImagebrush
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
DrawingImagepen
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
WpfCast<T>(object)
Cast value to specified type.
public static T WpfCast<T>(this object value)
Parameters
value
objectSource value.
Returns
- T
Casted value.
Type Parameters
T
Return type.