|
@@ -31,6 +31,115 @@ public class FuncTemplateSelector : DataTemplateSelector
|
|
|
|
|
|
public static class WPFUtils
|
|
public static class WPFUtils
|
|
{
|
|
{
|
|
|
|
+ public static Style AddSetter(this Style style, DependencyProperty property, object value)
|
|
|
|
+ {
|
|
|
|
+ style.Setters.Add(new Setter(property, value));
|
|
|
|
+ return style;
|
|
|
|
+ }
|
|
|
|
+ public static DataTrigger AddSetter(this DataTrigger trigger, DependencyProperty property, object value)
|
|
|
|
+ {
|
|
|
|
+ trigger.Setters.Add(new Setter(property, value));
|
|
|
|
+ return trigger;
|
|
|
|
+ }
|
|
|
|
+ public static DataTrigger AddDataTrigger(this Style style)
|
|
|
|
+ {
|
|
|
|
+ var trigger = new DataTrigger();
|
|
|
|
+ style.Triggers.Add(trigger);
|
|
|
|
+ return trigger;
|
|
|
|
+ }
|
|
|
|
+ public static DataTrigger AddDataTrigger(this Style style, Binding binding, object value)
|
|
|
|
+ {
|
|
|
|
+ var trigger = new DataTrigger() { Binding = binding, Value = value };
|
|
|
|
+ style.Triggers.Add(trigger);
|
|
|
|
+ return trigger;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static SolidColorBrush ToBrush(this System.Windows.Media.Color color)
|
|
|
|
+ {
|
|
|
|
+ return new SolidColorBrush(color);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ #region Binding
|
|
|
|
+
|
|
|
|
+ public static Binding CreateBinding<T, TProperty>(
|
|
|
|
+ T source,
|
|
|
|
+ Expression<Func<T, TProperty>> expression,
|
|
|
|
+ IValueConverter? converter,
|
|
|
|
+ string? format)
|
|
|
|
+ {
|
|
|
|
+ return new Binding(CoreUtils.GetFullPropertyName(expression, "_"))
|
|
|
|
+ {
|
|
|
|
+ Source = source,
|
|
|
|
+ Converter = converter,
|
|
|
|
+ StringFormat = format
|
|
|
|
+ };
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static Binding CreateBinding<T, TProperty>(
|
|
|
|
+ Expression<Func<T, TProperty>> expression,
|
|
|
|
+ IValueConverter? converter,
|
|
|
|
+ BindingMode mode,
|
|
|
|
+ string? format)
|
|
|
|
+ {
|
|
|
|
+ return new Binding(CoreUtils.GetFullPropertyName(expression, "_"))
|
|
|
|
+ {
|
|
|
|
+ Converter = converter,
|
|
|
|
+ StringFormat = format,
|
|
|
|
+ Mode = mode
|
|
|
|
+ };
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static DataTrigger Bind<T, TProperty, TValue>(
|
|
|
|
+ this DataTrigger trigger,
|
|
|
|
+ T source,
|
|
|
|
+ Expression<Func<T, TProperty>> expression,
|
|
|
|
+ TValue value,
|
|
|
|
+ IValueConverter<TProperty, TValue>? converter,
|
|
|
|
+ string? format = null)
|
|
|
|
+ {
|
|
|
|
+ trigger.Binding = CreateBinding(source, expression, converter, format);
|
|
|
|
+ trigger.Value = value;
|
|
|
|
+ return trigger;
|
|
|
|
+ }
|
|
|
|
+ public static DataTrigger Bind<T, TProperty, TValue>(
|
|
|
|
+ this DataTrigger trigger,
|
|
|
|
+ Expression<Func<T, TProperty>> expression,
|
|
|
|
+ TValue value,
|
|
|
|
+ IValueConverter<TProperty, TValue>? converter,
|
|
|
|
+ BindingMode mode = BindingMode.Default,
|
|
|
|
+ string? format = null)
|
|
|
|
+ {
|
|
|
|
+ trigger.Binding = CreateBinding(expression, converter, mode, format);
|
|
|
|
+ trigger.Value = value;
|
|
|
|
+ return trigger;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static DataTrigger Bind<T, TProperty>(
|
|
|
|
+ this DataTrigger trigger,
|
|
|
|
+ T source,
|
|
|
|
+ Expression<Func<T, TProperty>> expression,
|
|
|
|
+ TProperty value,
|
|
|
|
+ IValueConverter? converter = null,
|
|
|
|
+ string? format = null)
|
|
|
|
+ {
|
|
|
|
+ trigger.Binding = CreateBinding(source, expression, converter, format);
|
|
|
|
+ trigger.Value = value;
|
|
|
|
+ return trigger;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static DataTrigger Bind<T, TProperty>(
|
|
|
|
+ this DataTrigger trigger,
|
|
|
|
+ Expression<Func<T, TProperty>> expression,
|
|
|
|
+ TProperty value,
|
|
|
|
+ IValueConverter? converter = null,
|
|
|
|
+ BindingMode mode = BindingMode.Default,
|
|
|
|
+ string? format = null)
|
|
|
|
+ {
|
|
|
|
+ trigger.Binding = CreateBinding(expression, converter, mode, format);
|
|
|
|
+ trigger.Value = value;
|
|
|
|
+ return trigger;
|
|
|
|
+ }
|
|
|
|
+
|
|
public static void Bind<T, TProperty>(
|
|
public static void Bind<T, TProperty>(
|
|
this FrameworkElement element,
|
|
this FrameworkElement element,
|
|
DependencyProperty property,
|
|
DependencyProperty property,
|
|
@@ -41,12 +150,7 @@ public static class WPFUtils
|
|
{
|
|
{
|
|
element.SetBinding(
|
|
element.SetBinding(
|
|
property,
|
|
property,
|
|
- new Binding(CoreUtils.GetFullPropertyName(expression, "_"))
|
|
|
|
- {
|
|
|
|
- Source = source,
|
|
|
|
- Converter = converter,
|
|
|
|
- StringFormat = format
|
|
|
|
- }
|
|
|
|
|
|
+ CreateBinding(source, expression, converter, format)
|
|
);
|
|
);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -60,16 +164,13 @@ public static class WPFUtils
|
|
{
|
|
{
|
|
element.SetBinding(
|
|
element.SetBinding(
|
|
property,
|
|
property,
|
|
- new Binding(CoreUtils.GetFullPropertyName(expression, "_"))
|
|
|
|
- {
|
|
|
|
- Converter = converter,
|
|
|
|
- StringFormat = format,
|
|
|
|
- Mode = mode
|
|
|
|
- }
|
|
|
|
|
|
+ CreateBinding(expression, converter, mode, format)
|
|
);
|
|
);
|
|
}
|
|
}
|
|
-
|
|
|
|
-
|
|
|
|
|
|
+
|
|
|
|
+ #endregion
|
|
|
|
+
|
|
|
|
+
|
|
public static T? FindLogicalParent<T>(this DependencyObject dependencyObject)
|
|
public static T? FindLogicalParent<T>(this DependencyObject dependencyObject)
|
|
where T : DependencyObject
|
|
where T : DependencyObject
|
|
{
|
|
{
|
|
@@ -81,6 +182,37 @@ public static class WPFUtils
|
|
return parent as T;
|
|
return parent as T;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ public static IEnumerable<T> FindVisualChildren<T>(this DependencyObject depObj)
|
|
|
|
+ {
|
|
|
|
+ if (depObj != null)
|
|
|
|
+ //ContentControl cc = depObj as ContentControl;
|
|
|
|
+ //if (cc != null)
|
|
|
|
+ //{
|
|
|
|
+ // if (cc.Content == null)
|
|
|
|
+ // yield return null;
|
|
|
|
+ // if (cc.Content is T)
|
|
|
|
+ // yield return cc.Content as T;
|
|
|
|
+ // foreach (var child in FindVisualChildren<T>(cc.Content as DependencyObject))
|
|
|
|
+ // yield return child;
|
|
|
|
+ //}
|
|
|
|
+ //else
|
|
|
|
+ for (var i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
|
|
|
|
+ {
|
|
|
|
+ var child = VisualTreeHelper.GetChild(depObj, i);
|
|
|
|
+ if (child is null)
|
|
|
|
+ continue;
|
|
|
|
+
|
|
|
|
+ if (child is T t)
|
|
|
|
+ yield return t;
|
|
|
|
+
|
|
|
|
+ foreach (var childOfChild in FindVisualChildren<T>(child))
|
|
|
|
+ yield return childOfChild;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ #region Grid Children
|
|
|
|
+
|
|
public static int GetRow(this Grid grid, DependencyObject dependencyObject)
|
|
public static int GetRow(this Grid grid, DependencyObject dependencyObject)
|
|
{
|
|
{
|
|
while (true)
|
|
while (true)
|
|
@@ -147,33 +279,7 @@ public static class WPFUtils
|
|
return grid;
|
|
return grid;
|
|
}
|
|
}
|
|
|
|
|
|
- public static IEnumerable<T> FindVisualChildren<T>(this DependencyObject depObj)
|
|
|
|
- {
|
|
|
|
- if (depObj != null)
|
|
|
|
- //ContentControl cc = depObj as ContentControl;
|
|
|
|
- //if (cc != null)
|
|
|
|
- //{
|
|
|
|
- // if (cc.Content == null)
|
|
|
|
- // yield return null;
|
|
|
|
- // if (cc.Content is T)
|
|
|
|
- // yield return cc.Content as T;
|
|
|
|
- // foreach (var child in FindVisualChildren<T>(cc.Content as DependencyObject))
|
|
|
|
- // yield return child;
|
|
|
|
- //}
|
|
|
|
- //else
|
|
|
|
- for (var i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
|
|
|
|
- {
|
|
|
|
- var child = VisualTreeHelper.GetChild(depObj, i);
|
|
|
|
- if (child is null)
|
|
|
|
- continue;
|
|
|
|
-
|
|
|
|
- if (child is T t)
|
|
|
|
- yield return t;
|
|
|
|
-
|
|
|
|
- foreach (var childOfChild in FindVisualChildren<T>(child))
|
|
|
|
- yield return childOfChild;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ #endregion
|
|
|
|
|
|
#region Grid Columns + Rows
|
|
#region Grid Columns + Rows
|
|
|
|
|