Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function Get-VisualChildren($item) {
for ($i = 0; $i -lt [System.Windows.Media.VisualTreeHelper]::GetChildrenCount($item); $i++) {
$child = [System.Windows.Media.VisualTreeHelper]::GetChild($item, $i)
Get-VisualChildren($child)
}
$item
}
function Get-TreeItems {
Get-VisualChildren $snoopui | ? { $_.GetType().Name -eq "ProperTreeViewItem" }
}
function ShowMessage($treeItem) {
[system.windows.messagebox]::show($treeItem.ToString());
}
function Set-RightClick($item) {
#$action = { [system.windows.messagebox]::show($msg) }.GetNewClosure()
#$action = [ScriptBlock]::Create('[system.windows.messagebox]::show("' + $msg + '")')
#Register-ObjectEvent $item "MouseRightButtonDown" -Action { ShowMessage $Sender; $Event.SourceArgs[1].Handled = $true }
$handler = [Windows.Input.MouseButtonEventHandler]{ ShowMessage $this; $_.Handled = $true; }
$item.Add_MouseRightButtonDown($handler)
}
function Show-Message([string]$msg = 'hello world') {
$action = { [system.windows.messagebox]::show($msg) }.GetNewClosure()
$action
}
Add-Type @"
public class DelegateCommand : System.Windows.Input.ICommand
{
private System.Action<object> _action;
public DelegateCommand(System.Action<object> action)
{
_action = action;
}
public bool CanExecute(object parameter)
{
return true;
}
public event System.EventHandler CanExecuteChanged = delegate { };
public void Execute(object parameter)
{
_action(parameter);
}
}
"@
function Set-ContextMenu() {
$menuItems = ( @{Item="Add Child Element";Command = New-Object DelegateCommand( { Add-XamlVisual } ) },
@{Item="Delete";Command = New-Object DelegateCommand( { Delete-Visual } ) } )
Get-TreeItems | % {
$menu = (New-Object System.Windows.Controls.ContextMenu)
foreach ($menuItem in $menuItems) {
$item = New-Object System.Windows.Controls.MenuItem
$item.Header = $menuItem.Item
$item.Command = $menuItem.Command
$menu.Items.Add($item) | Out-Null
}
$_.ContextMenu = $menu
};
}
function Delete-Visual($item = $selected) {
$parent = $item.parent.visual
if ($parent.Content) { $parent.Content = $null }
elseif ($parent.Child) { $parent.Child = $null }
elseif ($parent.Children) { $parent.Children.Remove($item.visual) }
}
function Add-XamlVisual()
{
$xaml = Query-Xaml
Add-Visual($xaml)
}
function Create-Visual([string]$xaml) {
$pc = (new-object System.Windows.Markup.ParserContext)
$pc.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation")
[System.Windows.Markup.XamlReader]::Parse($xaml, $pc)
}
function Add-Visual([string]$xaml, [System.Windows.UIElement]$root = $selected.visual) {
$visual = Create-Visual $xaml
try {
$root.children.add($visual)
} catch { }
try {
$root.child = $visual
} catch { }
try {
$root.content = $visual
} catch {}
}
function Query-Xaml()
{
$w = new-object system.windows.window;
$w.Width = 400
$w.Height = 200
$w.content = new-object system.windows.controls.textbox;
$w.content.text = "<TextBlock>Type XAML here!</TextBlock>";
$w.showdialog() | Out-Null
$w.content.text
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.