XamlHelper
Miscellaneous WPF helper and extension methods for UI threading, bindings, imaging, dialogs, and type conversions.
Properties
public static Dispatcher CurrentThreadDispatcher { get; }
value = XamlHelper.CurrentThreadDispatcher
Gets the Dispatcher associated with the current thread.
Methods
public static void CopyToClipboard<T>(T value)
XamlHelper.CopyToClipboard(value)
Copies the provided value to the system clipboard on the UI thread.
public static void DeleteHideable(Window window)
XamlHelper.DeleteHideable(window)
Removes previously added hide-on-close behavior.
public static void EnsureUIThread(object obj)
XamlHelper.EnsureUIThread(obj)
Ensures the current thread is the UI thread and throws otherwise.
- obj
- Any DispatcherObject or Dispatcher or other object to infer dispatcher from.
public static void EnsureUIThread()
XamlHelper.EnsureUIThread()
Throws if the current thread is not the UI thread.
public static T FindLogicalChild<T>(DependencyObject obj)
result = XamlHelper.FindLogicalChild(obj)
Searches the logical tree depth-first and returns the first child of the specified type, or null if none found.
public static T FindLogicalParentOfType<T>(DependencyObject child)
result = XamlHelper.FindLogicalParentOfType(child)
Walks up the logical tree and returns the first parent of the specified type, or null if none found.
public static IEnumerable<T> FindVisualChilds<T>(DependencyObject obj)
result = XamlHelper.FindVisualChilds(obj)
Enumerates all visual children of the specified type.
public static T FindVisualChild<T>(DependencyObject obj)
result = XamlHelper.FindVisualChild(obj)
Returns the first visual child of the specified type, or null if none found.
public static VerticalAlignment FromWpf(VerticalAlignment value)
result = XamlHelper.FromWpf(value)
Convert VerticalAlignment value to VerticalAlignment.
- value
- VerticalAlignment value.
Returns: VerticalAlignment value.
public static PointF FromWpf(Point value)
result = XamlHelper.FromWpf(value)
Convert Point value to PointF.
- value
- Point value.
Returns: PointF value.
public static Brush FromWpf(Brush value)
result = XamlHelper.FromWpf(value)
Convert Brush value to DrawingBrush.
- value
- Brush value.
Returns: DrawingBrush value.
public static Color FromWpf(Color value)
result = XamlHelper.FromWpf(value)
Convert Color value to Color.
- value
- Color value.
Returns: Color value.
public static HorizontalAlignment FromWpf(HorizontalAlignment value)
result = XamlHelper.FromWpf(value)
Convert HorizontalAlignment value to HorizontalAlignment.
- value
- HorizontalAlignment value.
Returns: HorizontalAlignment value.
public static Thickness FromWpf(Thickness value)
result = XamlHelper.FromWpf(value)
Convert Thickness value to Thickness.
- value
- Thickness value.
Returns: Thickness value.
public static Window GetActiveOrMainWindow(Application app)
result = XamlHelper.GetActiveOrMainWindow(app)
Returns the active window or the main window if none is active.
public static Window GetActiveWindow(Application app)
result = XamlHelper.GetActiveWindow(app)
Returns the first active window, or null if none found.
public static IEnumerable<Window> GetActiveWindows(Application app)
result = XamlHelper.GetActiveWindows(app)
Returns all currently active windows in the application.
public static ValueTuple<int, int> GetActualSize(FrameworkElement elem)
result = XamlHelper.GetActualSize(elem)
Returns the element ActualWidth/ActualHeight as integers tuple.
public static Brush GetBrush(DrawingImage source)
result = XamlHelper.GetBrush(source)
Gets the first brush found in the DrawingImage's Drawing.
public static bool? GetDialogResult(DependencyObject obj)
result = XamlHelper.GetDialogResult(obj)
Boilerplate attached property "bool? DialogResult" used to set Window.DialogResult from a Button.
public static BitmapSource GetImage(FrameworkElement elem)
result = XamlHelper.GetImage(elem)
Renders a FrameworkElement into a bitmap using its current size.
public static BitmapSource GetImage(Visual visual, ValueTuple<int, int> size)
result = XamlHelper.GetImage(visual, size)
Renders a Visual into a bitmap using the specified width and height.
public static BitmapSource GetImage(Visual visual, int width, int height)
result = XamlHelper.GetImage(visual, width, height)
Renders a Visual into a bitmap with the given width and height.
public static ImageSource GetImage(string iconKey, SolidColorBrush fill, SolidColorBrush stroke)
result = XamlHelper.GetImage(iconKey, fill, stroke)
Gets a themed SVG/Vector icon recolored with the specified fill and stroke brushes.
public static IEnumerable<string> GetSystemFontFamilies()
result = XamlHelper.GetSystemFontFamilies()
Get system font families.
Returns: System font families.
public static Window GetWindow(DependencyObject obj)
result = XamlHelper.GetWindow(obj)
Gets the owning window for the specified object.
public static void GuiAsync(Dispatcher dispatcher, Action action, DispatcherPriority priority)
XamlHelper.GuiAsync(dispatcher, action, priority)
Executes the action on the UI thread asynchronously with the specified priority.
public static void GuiAsync(Dispatcher dispatcher, Action action)
XamlHelper.GuiAsync(dispatcher, action)
Executes the action on the UI thread asynchronously with normal priority.
public static void GuiAsync(DispatcherObject obj, Action action)
XamlHelper.GuiAsync(obj, action)
Executes the action on the UI thread asynchronously using the object's Dispatcher.
public static void GuiSync(Dispatcher dispatcher, Action action, DispatcherPriority priority)
XamlHelper.GuiSync(dispatcher, action, priority)
Executes the action synchronously on the Dispatcher with the specified priority.
public static void GuiSync(DispatcherObject obj, Action action)
XamlHelper.GuiSync(obj, action)
Executes the action synchronously on the object's Dispatcher.
public static void GuiSync(Dispatcher dispatcher, Action action)
XamlHelper.GuiSync(dispatcher, action)
Executes the action synchronously on the Dispatcher with normal priority.
public static T GuiSync<T>(Dispatcher dispatcher, Func<T> func)
result = XamlHelper.GuiSync(dispatcher, func)
Executes the function synchronously on the Dispatcher with normal priority and returns the result.
public static T GuiSync<T>(DispatcherObject obj, Func<T> func)
result = XamlHelper.GuiSync(obj, func)
Executes the function synchronously on the object's Dispatcher and returns its result.
public static T GuiSync<T>(Dispatcher dispatcher, Func<T> func, DispatcherPriority priority)
result = XamlHelper.GuiSync(dispatcher, func, priority)
Executes the function synchronously on the Dispatcher with the specified priority and returns the result.
public static Task GuiThreadGetAsync(Action<CancellationToken> action, CancellationToken token)
result = XamlHelper.GuiThreadGetAsync(action, token)
Invokes the provided action on the UI thread if required.
public static Task GuiThreadGetAsync(Func<CancellationToken, Task> func, CancellationToken token)
result = XamlHelper.GuiThreadGetAsync(func, token)
Invokes the provided async action on the UI thread if required.
public static Task<TResult> GuiThreadGetAsync<TResult>(Func<CancellationToken, TResult> func, CancellationToken token)
result = XamlHelper.GuiThreadGetAsync(func, token)
Invokes the provided function on the UI thread if required and returns its result.
public static Task<TResult> GuiThreadGetAsync<TResult>(Func<CancellationToken, Task<TResult>> func, CancellationToken token)
result = XamlHelper.GuiThreadGetAsync(func, token)
Invokes the provided async function on the UI thread if required and returns its result.
public static DispatcherOperation InvokeAsync(DependencyObject obj, Action action, DispatcherPriority priority, CancellationToken cancellationToken)
result = XamlHelper.InvokeAsync(obj, action, priority, cancellationToken)
Invokes the provided function on the UI thread if required.
- obj
- The dependency object to get the Dispatcher from.
- action
- The function to invoke.
- priority
- DispatcherPriority
- cancellationToken
- CancellationToken
Returns: DispatcherOperation
public static DispatcherOperation<T> InvokeAsync<T>(DependencyObject obj, Func<T> action, DispatcherPriority priority, CancellationToken cancellationToken)
result = XamlHelper.InvokeAsync(obj, action, priority, cancellationToken)
Invokes the provided function on the UI thread if required and returns its result.
- obj
- The dependency object to get the Dispatcher from.
- action
- The function to invoke.
- priority
- DispatcherPriority
- cancellationToken
- CancellationToken
Returns: DispatcherOperation
public static bool IsDesignMode(DependencyObject obj)
result = XamlHelper.IsDesignMode(obj)
Returns true if running in design mode.
public static bool IsFontSupported(string familyName)
result = XamlHelper.IsFontSupported(familyName)
Determines if font is supported.
- familyName
- Font family name.
Returns: True if supported; otherwise false.
public static bool IsUI()
result = XamlHelper.IsUI()
Determines the current thread is associated with Dispatcher.
Returns: Check result.
public static void LoadViewFromUri(Control control, string baseUri)
XamlHelper.LoadViewFromUri(control, baseUri)
Loads a XAML view from the specified relative pack URI into the control.
public static void MakeHideable(Window window)
XamlHelper.MakeHideable(window)
Hooks Closing event to hide the window instead of closing it.
public static SvgImage ReColor(SvgImage orig, Color? stroke, Color? fill)
result = XamlHelper.ReColor(orig, stroke, fill)
Change SVG image stroke/fill colors. Original is not changed.
public static void SaveImage(BitmapSource image, Stream file)
XamlHelper.SaveImage(image, file)
Saves the bitmap into the provided stream in PNG format.
public static void SetBindings(DependencyObject obj, DependencyProperty property, object dataObject, string path, BindingMode mode, IValueConverter converter, object parameter)
XamlHelper.SetBindings(obj, property, dataObject, path, mode, converter, parameter)
Applies a simple single binding to the specified dependency property.
public static void SetBindings(DependencyObject obj, DependencyProperty property, object dataObject, PropertyPath path, BindingMode mode, IValueConverter converter, object parameter)
XamlHelper.SetBindings(obj, property, dataObject, path, mode, converter, parameter)
Applies a simple single binding to the specified dependency property with a PropertyPath.
public static void SetDialogResult(DependencyObject obj, bool? value)
XamlHelper.SetDialogResult(obj, value)
Sets the attached DialogResult value for a button.
public static void SetMultiBinding(DependencyObject obj, DependencyProperty prop, IMultiValueConverter conv, Binding[] bindings)
XamlHelper.SetMultiBinding(obj, prop, conv, bindings)
Creates a MultiBinding with provided bindings and converter and applies it to the specified property.
public static bool ShowModal(CommonDialog dlg, Window owner)
result = XamlHelper.ShowModal(dlg, owner)
Shows the CommonDialog as modal with the specified owner window.
public static bool ShowModal(CommonDialog dlg, DependencyObject obj)
result = XamlHelper.ShowModal(dlg, obj)
Shows the CommonDialog as modal using the owning object to resolve the owner window.
public static bool ShowModal(Window wnd, Window owner)
result = XamlHelper.ShowModal(wnd, owner)
Shows the window as modal with the specified owner.
public static bool ShowModal(Window wnd, DependencyObject obj)
result = XamlHelper.ShowModal(wnd, obj)
Shows the window as modal using the owning object to resolve the owner window.
public static bool ShowModal(Window wnd)
result = XamlHelper.ShowModal(wnd)
Shows the window as modal using Application.Current.MainWindow as owner if available.
public static bool ShowModel<TWindow>(object model, DependencyObject owner)
result = XamlHelper.ShowModel(model, owner)
Creates and shows a modal Window of type TWindow with DataContext set to the provided model.
public static BitmapImage ToBitmapImage(Stream streamSource, BitmapCreateOptions options, BitmapCacheOption cache, bool freeze)
result = XamlHelper.ToBitmapImage(streamSource, options, cache, freeze)
Creates a BitmapImage from a stream with optional init parameters.
public static Color ToColor(int color)
result = XamlHelper.ToColor(color)
Converts ARGB int to Color.
public static int ToInt(Color color)
result = XamlHelper.ToInt(color)
Converts a Color to ARGB int.
public static ImageSource ToSvg(Stream stream)
result = XamlHelper.ToSvg(stream)
Convert stream to SVG image.
- stream
- Stream
Returns: SVG image.
public static Color ToTransparent(Color color, byte alpha)
result = XamlHelper.ToTransparent(color, alpha)
Returns a copy of the color with the specified alpha channel.
public static VerticalAlignment ToWpf(VerticalAlignment value)
result = XamlHelper.ToWpf(value)
Convert VerticalAlignment value to VerticalAlignment.
- value
- VerticalAlignment value.
Returns: VerticalAlignment value.
public static HorizontalAlignment ToWpf(HorizontalAlignment value)
result = XamlHelper.ToWpf(value)
Convert HorizontalAlignment value to HorizontalAlignment.
- value
- HorizontalAlignment value.
Returns: HorizontalAlignment value.
public static Thickness ToWpf(Thickness value)
result = XamlHelper.ToWpf(value)
Convert Thickness value to Thickness.
- value
- Thickness value.
Returns: Thickness value.
public static Point ToWpf(PointF value)
result = XamlHelper.ToWpf(value)
Convert PointF value to Point.
- value
- PointF value.
Returns: Point value.
public static Brush ToWpf(Brush value)
result = XamlHelper.ToWpf(value)
Convert DrawingBrush value to Brush.
- value
- DrawingBrush value.
Returns: Brush value.
public static Color ToWpf(Color value)
result = XamlHelper.ToWpf(value)
Convert Color value to Color.
- value
- Color value.
Returns: Color value.
public static void TryOpenLink(string url, DependencyObject owner)
XamlHelper.TryOpenLink(url, owner)
Try to open the specified link. If it fails, copies the URL to clipboard and shows an error message.
- url
- Link.
- owner
- UI thread owner.
public static bool TryOpenWithInitialDir(DXFileDialog dlg, DependencyObject owner, string ctrlName)
result = XamlHelper.TryOpenWithInitialDir(dlg, owner, ctrlName)
Opens a file dialog using last used directory (if available) and stores the selected directory on close.
public static bool TryOpenWithInitialDir(DXFolderBrowserDialog dlg, DependencyObject owner, string ctrlName)
result = XamlHelper.TryOpenWithInitialDir(dlg, owner, ctrlName)
Opens a folder browser dialog using last used directory (if available) and stores the selected directory on close.
public static void UpdateBrush(DrawingImage source, Brush brush)
XamlHelper.UpdateBrush(source, brush)
Updates all brushes inside the drawing image to the specified brush.
public static void UpdatePen(DrawingImage source, Pen pen)
XamlHelper.UpdatePen(source, pen)
Updates all pens inside the drawing image to the specified pen.
public static ImageSource Url2Img(Uri uri)
result = XamlHelper.Url2Img(uri)
Creates an ImageSource from a pack or http(s) URI. Supports SVG via DevExpress.
public static T WpfCast<T>(object value)
result = XamlHelper.WpfCast(value)
Cast value to specified type.
- value
- Source value.
Returns: Casted value.
Fields
public static readonly DependencyProperty DialogResultProperty
value = XamlHelper.DialogResultProperty
Attached property that wires up a Button to set its owning window DialogResult when clicked.
public static readonly ICommand DisabledCommand
value = XamlHelper.DisabledCommand
Disabled command returning false from CanExecute and throwing when executed.