mirror of
https://github.com/ckaczor/Common.Wpf.git
synced 2026-01-13 17:22:47 -05:00
Initial commit
This commit is contained in:
111
Extensions/ApplicationExtensions.cs
Normal file
111
Extensions/ApplicationExtensions.cs
Normal file
@@ -0,0 +1,111 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Security;
|
||||
using System.Security.Permissions;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.ConstrainedExecution;
|
||||
using System.Deployment.Application;
|
||||
using Microsoft.Win32;
|
||||
|
||||
namespace Common.Wpf.Extensions
|
||||
{
|
||||
public enum HostType
|
||||
{
|
||||
HostTypeDefault = 0x0,
|
||||
HostTypeAppLaunch = 0x1,
|
||||
HostTypeCorFlag = 0x2
|
||||
}
|
||||
|
||||
// Taken from System.Windows.Forms.UnsafeNativeMethods
|
||||
[StructLayout(LayoutKind.Sequential), SuppressUnmanagedCodeSecurity]
|
||||
internal class ProcessInformation
|
||||
{
|
||||
public IntPtr hProcess = IntPtr.Zero;
|
||||
public IntPtr hThread = IntPtr.Zero;
|
||||
public int dwProcessId;
|
||||
public int dwThreadId;
|
||||
private static readonly IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
|
||||
~ProcessInformation()
|
||||
{
|
||||
close();
|
||||
}
|
||||
|
||||
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
|
||||
internal void close()
|
||||
{
|
||||
if ((hProcess != IntPtr.Zero) && (hProcess != INVALID_HANDLE_VALUE))
|
||||
{
|
||||
CloseHandle(new HandleRef(this, hProcess));
|
||||
hProcess = INVALID_HANDLE_VALUE;
|
||||
}
|
||||
if ((hThread != IntPtr.Zero) && (hThread != INVALID_HANDLE_VALUE))
|
||||
{
|
||||
CloseHandle(new HandleRef(this, hThread));
|
||||
hThread = INVALID_HANDLE_VALUE;
|
||||
}
|
||||
}
|
||||
|
||||
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
|
||||
private static extern bool CloseHandle(HandleRef handle);
|
||||
}
|
||||
|
||||
public static class ApplicationExtensions
|
||||
{
|
||||
[DllImport("clr.dll", CharSet = CharSet.Unicode, ExactSpelling = true, PreserveSig = false)]
|
||||
internal static extern void CorLaunchApplication(uint hostType, string applicationFullName, int manifestPathsCount, string[] manifestPaths, int activationDataCount, string[] activationData, ProcessInformation processInformation);
|
||||
|
||||
// Originally from System.Windows.Forms.Application, changed to suit needs
|
||||
[SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode), SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
|
||||
public static void Restart(this System.Windows.Application application)
|
||||
{
|
||||
if (Assembly.GetEntryAssembly() == null)
|
||||
{
|
||||
throw new NotSupportedException("RestartNotSupported");
|
||||
}
|
||||
if (ApplicationDeployment.IsNetworkDeployed)
|
||||
{
|
||||
string updatedApplicationFullName = ApplicationDeployment.CurrentDeployment.UpdatedApplicationFullName;
|
||||
|
||||
if (System.Windows.Application.Current != null)
|
||||
System.Windows.Application.Current.Shutdown();
|
||||
|
||||
CorLaunchApplication((uint) HostType.HostTypeDefault, updatedApplicationFullName, 0, null, 0, null, new ProcessInformation());
|
||||
}
|
||||
}
|
||||
|
||||
public static void SetStartWithWindows(this System.Windows.Application application, bool value)
|
||||
{
|
||||
string applicationName = Assembly.GetEntryAssembly().GetName().Name;
|
||||
|
||||
SetStartWithWindows(application, applicationName, value);
|
||||
}
|
||||
|
||||
public static void SetStartWithWindows(this System.Windows.Application application, string applicationName, bool value)
|
||||
{
|
||||
string applicationPath = string.Format("\"{0}\"", Assembly.GetEntryAssembly().Location);
|
||||
|
||||
SetStartWithWindows(application, applicationName, applicationPath, value);
|
||||
}
|
||||
|
||||
public static void SetStartWithWindows(this System.Windows.Application application, string applicationName, string applicationPath, bool value)
|
||||
{
|
||||
// Open the regsitry key
|
||||
RegistryKey regkey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
|
||||
|
||||
// If we couldn't get the key then stop
|
||||
if (regkey == null)
|
||||
return;
|
||||
|
||||
// Delete any existing key
|
||||
regkey.DeleteValue(applicationName, false);
|
||||
|
||||
// If auto start should not be on then we're done
|
||||
if (!value)
|
||||
return;
|
||||
|
||||
// Set the registry key
|
||||
regkey.SetValue(applicationName, applicationPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
20
Extensions/DependencyObjectExtensions.cs
Normal file
20
Extensions/DependencyObjectExtensions.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using System.Windows;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace Common.Wpf.Extensions
|
||||
{
|
||||
public static class DependencyObjectExtensions
|
||||
{
|
||||
public static T GetAncestor<T>(this DependencyObject referenceObject) where T : class
|
||||
{
|
||||
DependencyObject parent = VisualTreeHelper.GetParent(referenceObject);
|
||||
|
||||
while (parent != null && !parent.GetType().Equals(typeof(T)))
|
||||
{
|
||||
parent = VisualTreeHelper.GetParent(parent);
|
||||
}
|
||||
|
||||
return parent as T;
|
||||
}
|
||||
}
|
||||
}
|
||||
43
Extensions/FontExtensions.cs
Normal file
43
Extensions/FontExtensions.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Windows.Media;
|
||||
using Microsoft.Win32;
|
||||
|
||||
namespace Common.Wpf.Extensions
|
||||
{
|
||||
public static class FontExtensions
|
||||
{
|
||||
public static bool IsComposite(FontFamily fontFamily)
|
||||
{
|
||||
return fontFamily.Source.StartsWith("Global");
|
||||
}
|
||||
|
||||
public static bool IsSymbol(FontFamily fontFamily)
|
||||
{
|
||||
Typeface typeface = fontFamily.GetTypefaces().First();
|
||||
GlyphTypeface glyph;
|
||||
typeface.TryGetGlyphTypeface(out glyph);
|
||||
return glyph.Symbol;
|
||||
}
|
||||
|
||||
public static bool IsVisible(FontFamily fontFamily)
|
||||
{
|
||||
return !IsHidden(fontFamily);
|
||||
}
|
||||
|
||||
public static bool IsHidden(FontFamily fontFamily)
|
||||
{
|
||||
const string fontManagementKey = @"Software\Microsoft\Windows NT\CurrentVersion\Font Management";
|
||||
const string inactiveFontsValue = "Inactive Fonts";
|
||||
|
||||
RegistryKey key = Registry.CurrentUser.OpenSubKey(fontManagementKey);
|
||||
|
||||
if (key == null)
|
||||
return false;
|
||||
|
||||
IEnumerable<string> hiddenFonts = (string[]) key.GetValue(inactiveFontsValue);
|
||||
|
||||
return hiddenFonts.Contains(fontFamily.Source);
|
||||
}
|
||||
}
|
||||
}
|
||||
18
Extensions/GridExtensions.cs
Normal file
18
Extensions/GridExtensions.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using System.Windows.Controls;
|
||||
|
||||
namespace Common.Wpf.Extensions
|
||||
{
|
||||
public static class GridExtensions
|
||||
{
|
||||
public static int GetRowIndex(this Grid grid, RowDefinition rowDefinition)
|
||||
{
|
||||
for (int i = 0; i < grid.RowDefinitions.Count; i++)
|
||||
{
|
||||
if (grid.RowDefinitions[i] == rowDefinition)
|
||||
return i;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
103
Extensions/WindowExtensions.cs
Normal file
103
Extensions/WindowExtensions.cs
Normal file
@@ -0,0 +1,103 @@
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace Common.Wpf.Extensions
|
||||
{
|
||||
public static class WindowExtensions
|
||||
{
|
||||
public static Dictionary<FrameworkElement, BindingExpression> GetExplicitBindingExpressions(this DependencyObject parent)
|
||||
{
|
||||
// Create a dictionary of framework elements and binding expressions
|
||||
Dictionary<FrameworkElement, BindingExpression> bindingExpressions = new Dictionary<FrameworkElement, BindingExpression>();
|
||||
|
||||
// Get all explict bindings into the list
|
||||
GetExplicitBindingExpressions(parent, ref bindingExpressions);
|
||||
|
||||
return bindingExpressions;
|
||||
}
|
||||
|
||||
private static void GetExplicitBindingExpressions(DependencyObject parent, ref Dictionary<FrameworkElement, BindingExpression> bindingExpressions)
|
||||
{
|
||||
// Get the number of children
|
||||
int childCount = VisualTreeHelper.GetChildrenCount(parent);
|
||||
|
||||
// Loop over each child
|
||||
for (int childIndex = 0; childIndex < childCount; childIndex++)
|
||||
{
|
||||
// Get the child
|
||||
DependencyObject dependencyObject = VisualTreeHelper.GetChild(parent, childIndex);
|
||||
|
||||
// Check if the object is a tab control
|
||||
if (dependencyObject is TabControl)
|
||||
{
|
||||
// Cast the tab control
|
||||
TabControl tabControl = (dependencyObject as TabControl);
|
||||
|
||||
// Loop over each tab
|
||||
foreach (TabItem tabItem in tabControl.Items)
|
||||
GetExplicitBindingExpressions((DependencyObject) tabItem.Content, ref bindingExpressions);
|
||||
}
|
||||
else
|
||||
{
|
||||
// See if the child is a framework element
|
||||
if (dependencyObject is FrameworkElement)
|
||||
{
|
||||
// Cast to framework element
|
||||
FrameworkElement frameworkElement = (FrameworkElement) dependencyObject;
|
||||
|
||||
// Get the list of properties
|
||||
IEnumerable<DependencyProperty> dependencyProperties = (from PropertyDescriptor propertyDescriptor in TypeDescriptor.GetProperties(dependencyObject)
|
||||
select DependencyPropertyDescriptor.FromProperty(propertyDescriptor)
|
||||
into dependencyPropertyDescriptor
|
||||
where dependencyPropertyDescriptor != null
|
||||
select dependencyPropertyDescriptor.DependencyProperty).ToList();
|
||||
|
||||
// Loop over each dependency property in the list
|
||||
foreach (DependencyProperty dependencyProperty in dependencyProperties)
|
||||
{
|
||||
// Try to get the binding expression for the property
|
||||
BindingExpression bindingExpression = frameworkElement.GetBindingExpression(dependencyProperty);
|
||||
|
||||
// If there is a binding expression and it is set to explicit then make it update the source
|
||||
if (bindingExpression != null && bindingExpression.ParentBinding.UpdateSourceTrigger == UpdateSourceTrigger.Explicit)
|
||||
bindingExpressions.Add(frameworkElement, bindingExpression);
|
||||
}
|
||||
}
|
||||
|
||||
// If the dependency object has any children then check them
|
||||
if (VisualTreeHelper.GetChildrenCount(dependencyObject) > 0)
|
||||
GetExplicitBindingExpressions(dependencyObject, ref bindingExpressions);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void UpdateAllSources(this DependencyObject window)
|
||||
{
|
||||
UpdateAllSources(window, GetExplicitBindingExpressions(window).Values);
|
||||
}
|
||||
|
||||
public static void UpdateAllSources(this DependencyObject window, IEnumerable<BindingExpression> bindingExpressions)
|
||||
{
|
||||
foreach (var expression in bindingExpressions)
|
||||
expression.UpdateSource();
|
||||
}
|
||||
|
||||
public static void ClearAllValidationErrors(this DependencyObject window)
|
||||
{
|
||||
ClearAllValidationErrors(window, GetExplicitBindingExpressions(window).Values);
|
||||
}
|
||||
|
||||
public static void ClearAllValidationErrors(this DependencyObject window, IEnumerable<BindingExpression> bindingExpressions)
|
||||
{
|
||||
foreach (var expression in bindingExpressions)
|
||||
Validation.ClearInvalid(expression);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user