Class wpfBuilder
With this class you can create windows with controls, for example for data input.
public class wpfBuilder
Remarks
This class uses WPF (Windows Presentation Foundation). Creates window at run time. No designer. No WPF and XAML knowledge required, unless you want something advanced.
To start, use snippet wpfDialogSnippet or menu File > New > Dialogs. Also look in Cookbook.
Most functions return this
, to enable method chaining, aka fluent interface, like with StringBuilder. See example.
A wpfBuilder object can be used to create whole window or some window part, for example a tab page.
The size/position unit in WPF is about 1/96 inch, regardless of screen DPI. For example, if DPI is 96 (100%), 1 unit = 1 physical pixel; if 150% - 1.5 pixel; if 200% - 2 pixels. WPF windows are DPI-scaled automatically when need. Your program's manifest should contain dpiAware=true/PM
and dpiAwareness=PerMonitorV2
; it is default for scripts/programs created with the script editor of this library.
Note: WPF starts slowly and uses much memory. It is normal if to show the first window in process takes 500-1000 ms and the process uses 30 MB of memory, whereas WinForms takes 250 ms / 10 MB and native takes 50 ms / 2 MB. However WinForms becomes slower than WPF if there are more than 100 controls in window. This library uses WPF because it is the most powerful and works well with high DPI screens.
WPF has many control types, for example System.Windows.Controls.Button, System.Windows.Controls.CheckBox, System.Windows.Controls.TextBox, System.Windows.Controls.ComboBox, System.Windows.Controls.Label. Most are in namespaces System.Windows.Controls and System.Windows.Controls.Primitives. Also on the internet you can find many libraries containing WPF controls and themes. For example, search for github awesome dotnet C#. Many libraries are open-source, and most can be found in GitHub (source, info and sometimes compiled files). Compiled files usually can be found in https://www.nuget.org/ as packages. Use menu Tools > NuGet.
By default don't need XAML. When need, you can load XAML strings and files with System.Windows.Markup.XamlReader.
Examples
Dialog window with several controls for data input.
var b = new wpfBuilder("Example").WinSize(400); //create Window object with Grid control; set window width 400
b.R.Add("Text", out TextBox text1).Focus(); //add label and text box control in first row
b.R.Add("Combo", out ComboBox combo1).Items("One|Two|Three"); //in second row add label and combo box control with items
b.R.Add(out CheckBox c1, "Check"); //in third row add check box control
b.R.AddOkCancel(); //finally add standard OK and Cancel buttons
b.End();
if (!b.ShowDialog()) return; //show the dialog and wait until closed; return if closed not with OK button
print.it(text1.Text, combo1.SelectedIndex, c1.IsChecked == true); //get user input from control variables
Namespace: Au
Assembly: Au.dll
Constructors
Name | Description |
---|---|
wpfBuilder(string, WBPanelType) | This constructor creates System.Windows.Window object with panel of specified type (default is System.Windows.Controls.Grid). |
wpfBuilder(FrameworkElement, WBPanelType, bool) | This constructor creates panel of specified type (default is System.Windows.Controls.Grid) and optionally adds to a container. |
Properties
Name | Description |
---|---|
Last | Gets the last child or descendant element added in current panel. Before that returns current panel. |
Last2 | Gets the child or descendant element added in current panel before adding wpfBuilder.Last. Can be |
Panel | Gets current System.Windows.Controls.Grid or System.Windows.Controls.StackPanel or etc. |
R | Starts new auto-sized row in current grid. The same as |
ResultButton | If the window closed with an wpfBuilder.AddButton button, returns its result. Else returns 0. Note: if the button is in a tab page, use the wpfBuilder variable of that page. |
Window | Gets the top-level window. |
gridLines | System.Windows.Controls.Grid.ShowGridLines of grid panels created afterwards. To be used at design time only. |
winTopmost | System.Windows.Window.Topmost of windows created afterwards. Usually used at design time only, to make always on top of editor window. |
winWhite | If |
Methods
Name | Description |
---|---|
Add(object, FrameworkElement, WBGridLength?) | Adds 2 elements: System.Windows.Controls.Label and an existing element (control etc of any type). |
Add(FrameworkElement, WBAdd) | Adds an existing element (control etc of any type). |
AddButton(object, Action<WBButtonClickArgs>, WBBFlags) | Adds button with System.Windows.Controls.Primitives.ButtonBase.Click event handler. |
AddButton(object, int) | Adds button that closes the window and sets wpfBuilder.ResultButton. |
AddButton(out Button, object, Action<WBButtonClickArgs>, WBBFlags) | Adds button with System.Windows.Controls.Primitives.ButtonBase.Click event handler. |
AddOkCancel(string, string, string, bool?) | Adds OK and/or Cancel and/or Apply buttons. |
AddOkCancel(out Button, out Button, out Button, string, string, string, bool?) | Adds OK and/or Cancel and/or Apply buttons. |
AddSeparator(bool?) | Adds System.Windows.Controls.Separator control. |
Add<T>(object, WBAdd) | Creates and adds element of type T (any type). This overload can be used when don't need element's variable. |
Add<T>(object, out T, object, WBGridLength?) | Adds 2 elements: System.Windows.Controls.Label and element of type T (control etc of any type). |
Add<T>(out T, object, WBAdd) | Creates and adds element of type T (control etc of any type). |
Align(HorizontalAlignment?, VerticalAlignment?) | Sets horizontal and/or vertical alignment of the last added element. |
Align(string, string) | Sets horizontal and/or vertical alignment of the last added element. |
AlignContent(HorizontalAlignment?, VerticalAlignment?) | Sets content alignment of the last added element. |
AlignContent(string, string) | Sets content alignment of the last added element. |
AlsoAll(Action<wpfBuilder, WBAlsoAllArgs>) | Sets callback function to be called by AddX functions for each element added afterwards. Not called by StartX functions for panels. |
And(double) | Sets to add next element in the same grid cell as previous element. |
Bind(DependencyProperty, object, string) | Calls System.Windows.FrameworkElement.SetBinding of the last added element. Creates System.Windows.Data.Binding that uses source and path. |
Bind(DependencyProperty, string) | Calls System.Windows.FrameworkElement.SetBinding of the last added element. |
Bind(DependencyProperty, BindingBase) | Calls System.Windows.FrameworkElement.SetBinding of the last added element. |
Bind(DependencyProperty, BindingBase, out BindingExpressionBase) | Calls System.Windows.FrameworkElement.SetBinding of the last added element and gets its return value. |
BindingContext(object) | Sets System.Windows.FrameworkElement.DataContext property of the last added element. Then with wpfBuilder.Bind of this and descendant elements don't need to specify data source object because it is set by this function. |
Border(ColorInt, double, Thickness?, double?, Thickness?) | Sets border properties of the last added element, which can be Border or a Control-derived class. |
Border(Brush, double, Thickness?, double?, Thickness?) | Sets border properties of the last added element, which can be Border or a Control-derived class. |
Brush(ColorInt?, ColorInt?) | Sets background and/or foreground color of the last added element. |
Brush(Brush, Brush) | Sets background and/or foreground brush (color, gradient, etc) of the last added element. |
Checked(bool, RadioButton) | Sets System.Windows.Controls.Primitives.ToggleButton.IsChecked of the specified System.Windows.Controls.RadioButton. |
Checked(bool?, bool) | Sets System.Windows.Controls.Primitives.ToggleButton.IsChecked and System.Windows.Controls.Primitives.ToggleButton.IsThreeState of the last added check box or radio button. |
Columns(params WBGridLength[]) | Sets column count and widths of current grid. |
Disabled(bool) | Sets System.Windows.UIElement.IsEnabled of the last added element. |
Dock(Dock) | Docks the last added element in System.Windows.Controls.DockPanel. |
Editable() | Makes the last added System.Windows.Controls.ComboBox editable. |
End() | Ends adding controls etc to the window or nested panel (wpfBuilder.StartGrid etc). |
Focus() | Attempts to set focus to the last added element when it'll become visible. |
Font(string, double?, bool?, bool?) | Sets font properties of the last added element and its descendants. |
FormatText(InterpolatedString) | Adds inlines (text, formatted text, hyperlinks, images, etc) to the last added System.Windows.Controls.TextBlock etc. |
Height(WBLength, string) | Sets height of the last added element. Optionally sets alignment. |
Hidden(bool?) | Sets System.Windows.UIElement.Visibility of the last added element. |
Image(string, Stretch, StretchDirection) | Loads image from a file or URL into the last added System.Windows.Controls.Image. |
Image(ImageSource, Stretch, StretchDirection) | Loads image into the last added System.Windows.Controls.Image. |
Items(bool, Action<ComboBox>) | Sets callback function that should add items to the last added System.Windows.Controls.ComboBox later. |
Items(IEnumerable, bool) | Adds items as IEnumerable to the last added System.Windows.Controls.ItemsControl (System.Windows.Controls.ComboBox, etc), with "lazy" option. |
Items(params object[]) | Adds items of any type to the last added System.Windows.Controls.ItemsControl (System.Windows.Controls.ComboBox, etc). |
Items(string) | Splits string and ads substrings as items to the last added System.Windows.Controls.ItemsControl (System.Windows.Controls.ComboBox, etc). |
LabeledBy(bool?) | Makes wpfBuilder.Last2 behave as a label of wpfBuilder.Last. |
LabeledBy(FrameworkElement, bool?) | Makes an element behave as a label of the last added element (wpfBuilder.Last). |
LoadFile(string) | Loads a web page or RTF text from a file or URL into the last added element. |
Margin(double?, double?, double?, double?) | Sets margin of the last added element. |
Margin(string) | Sets margin of the last added element. |
Margin(Thickness) | Sets margin of the last added element. |
Multiline(WBLength?, TextWrapping) | Makes the last added System.Windows.Controls.TextBox multiline. |
Name(string, bool) | Sets System.Windows.FrameworkElement.Name of the last added element. |
Options(bool?, bool?, Thickness?, bool?, bool?) | Changes some options for elements added afterwards. |
Padding(double?, double?, double?, double?) | Sets padding of the last added control. |
Padding(string) | Sets padding of the last added control. |
Padding(Thickness) | Sets padding of the last added control. |
Readonly(bool, bool) | Sets System.Windows.Controls.Primitives.TextBoxBase.IsReadOnly or System.Windows.Controls.ComboBox.IsReadOnly of the last added text box or editable combo box. |
Row(WBGridLength) | Starts new row in current grid. |
Select(int) | Selects an item of the last added System.Windows.Controls.Primitives.Selector (System.Windows.Controls.ComboBox, etc). |
Select(object) | Selects an item of the last added System.Windows.Controls.Primitives.Selector (System.Windows.Controls.ComboBox, etc). |
ShowDialog(DependencyObject) | Shows the window and waits until closed. |
Size(WBLength, WBLength, string, string) | Sets width and height of the last added element. Optionally sets alignment. |
Skip(int) | Adds one or more empty cells in current row of current grid. |
Span(int) | Sets column span of the last added element. |
SpanRows(int) | Sets row span of the last added element. |
Splitter(bool, int, double) | Sets vertical or horizontal splitter properties of the last added System.Windows.Controls.GridSplitter. |
StartCanvas(bool) | Adds System.Windows.Controls.Canvas panel that will contain elements added with wpfBuilder.Add etc. Finally call wpfBuilder.End to return to current panel. |
StartCanvas<T>(object) | Adds a headered content control (System.Windows.Controls.GroupBox, System.Windows.Controls.Expander, etc) with child System.Windows.Controls.Canvas panel that will contain elements added with wpfBuilder.Add etc. Finally call wpfBuilder.End to return to current panel. |
StartCanvas<T>(out T, object) | Adds a headered content control (System.Windows.Controls.GroupBox, System.Windows.Controls.Expander, etc) with child System.Windows.Controls.Canvas panel that will contain elements added with wpfBuilder.Add etc. Finally call wpfBuilder.End to return to current panel. |
StartDock(bool) | Adds System.Windows.Controls.DockPanel panel that will contain elements added with wpfBuilder.Add etc. Finally call wpfBuilder.End to return to current panel. |
StartDock<T>(object) | Adds a headered content control (System.Windows.Controls.GroupBox, System.Windows.Controls.Expander, etc) with child System.Windows.Controls.DockPanel panel that will contain elements added with wpfBuilder.Add etc. Finally call wpfBuilder.End to return to current panel. |
StartDock<T>(out T, object) | Adds a headered content control (System.Windows.Controls.GroupBox, System.Windows.Controls.Expander, etc) with child System.Windows.Controls.DockPanel panel that will contain elements added with wpfBuilder.Add etc. Finally call wpfBuilder.End to return to current panel. |
StartGrid(bool) | Adds System.Windows.Controls.Grid panel (table) that will contain elements added with wpfBuilder.Add etc. Finally call wpfBuilder.End to return to current panel. |
StartGrid<T>(object) | Adds a headered content control (System.Windows.Controls.GroupBox, System.Windows.Controls.Expander, etc) with child System.Windows.Controls.Grid panel (table) that will contain elements added with wpfBuilder.Add etc. Finally call wpfBuilder.End to return to current panel. |
StartGrid<T>(out T, object) | Adds a headered content control (System.Windows.Controls.GroupBox, System.Windows.Controls.Expander, etc) with child System.Windows.Controls.Grid panel (table) that will contain elements added with wpfBuilder.Add etc. Finally call wpfBuilder.End to return to current panel. |
StartOkCancel() | Adds right-bottom-aligned horizontal stack panel (wpfBuilder.StartStack) for adding OK, Cancel and more buttons. When don't need more buttons, use just wpfBuilder.AddOkCancel. |
StartPanel(Panel, bool) | Adds panel of any type. It will contain elements added with wpfBuilder.Add etc. Finally call wpfBuilder.End to return to current panel. |
StartPanel<TContainer>(Panel, object) | Adds a headered content control (System.Windows.Controls.GroupBox, System.Windows.Controls.Expander, etc) with child panel of any type. The panel will contain elements added with wpfBuilder.Add etc. Finally call wpfBuilder.End to return to current panel. |
StartStack(bool, bool) | Adds System.Windows.Controls.StackPanel panel that will contain elements added with wpfBuilder.Add etc. Finally call wpfBuilder.End to return to current panel. |
StartStack<T>(object, bool) | Adds a headered content control (System.Windows.Controls.GroupBox, System.Windows.Controls.Expander, etc) with child System.Windows.Controls.StackPanel panel that will contain elements added with wpfBuilder.Add etc. Finally call wpfBuilder.End to return to current panel. |
StartStack<T>(out T, object, bool) | Adds a headered content control (System.Windows.Controls.GroupBox, System.Windows.Controls.Expander, etc) with child System.Windows.Controls.StackPanel panel that will contain elements added with wpfBuilder.Add etc. Finally call wpfBuilder.End to return to current panel. |
Tag(object) | Sets Tag property of the last added element. |
Tooltip(object) | Sets tooltip text/content/object of the last added element. See System.Windows.FrameworkElement.ToolTip. |
UiaName(string) | Sets UI Automation name of the last added element. |
Validation(Func<FrameworkElement, string>, Action<FrameworkElement>) | Sets a validation callback function for the last added element. |
Watermark(out WatermarkAdorner, string) | Sets watermark/hint/cue text of the last added TextBox or editable ComboBox control. The text is visible only when the control text is empty. |
Watermark(string) | Sets watermark/hint/cue text of the last added TextBox or editable ComboBox control. The text is visible only when the control text is empty. |
Width(WBLength, string) | Sets width of the last added element. Optionally sets alignment. |
WinProperties(WindowStartupLocation?, ResizeMode?, bool?, bool?, bool?, WindowState?, WindowStyle?, ImageSource, bool?) | Changes various window properties. |
WinRect(RECT) | Sets window rectangle (location and size). |
WinSaved(string, Action<string>) | Saves window xy/size/state when closing and restores when opening. |
WinSize(WBLength?, WBLength?) | Sets window width and/or height or/and min/max width/height. |
WinXY(int, int) | Sets window location. |
Wrap(bool) | Sets TextWrapping property of the last added element = TextWrapping.Wrap if |
Wrap(TextWrapping) | Sets TextWrapping property of the last added element. Supports TextBlock, TextBox and AccessText. |
XY(double, double, WBLength?, WBLength?) | Sets position of the last added element in Canvas panel. Optionally sets size. |
formatTextOf(object, InterpolatedString) | Adds inlines (text, formatted text, hyperlinks, images, etc) to the specified System.Windows.Controls.TextBlock etc. |
formattedText(InterpolatedString) | Creates new System.Windows.Controls.TextBlock and adds inlines like wpfBuilder.FormatText. |
Events
Name | Description |
---|---|
Loaded | When root panel loaded and visible. Once. |
OkApply | When clicked OK or Apply button. |