#wpf #powershell #xaml #button #grid
#wpf #powershell #xaml #кнопка #сетка
Вопрос:
Новое в Powershell, Xaml и кодировании в целом.
Благодаря StackOverflow я построил сетку в Powershell с 10 столбцами и 8 строками и заполнил ее 80 кнопками (от 1 до 80). Каждая кнопка запускает действие. Теперь я хотел бы иметь возможность нажать на 1 из этих кнопок и наложить новые кнопки (или целую новую сетку) поверх основной сетки, чтобы было доступно больше кнопок (от 81 до 160).
Чтобы сделать наглядный пример: давайте представим, что я создаю приложение для выполнения заказов в ресторане. В таблице 1 показано 80 кнопок. Если я нажму кнопку «Основное блюдо», она наложит 80 кнопок, содержащих 80 разных «основных блюд». То же самое, если я нажимаю кнопки «Десерт» и т. Д.
Я понял, как использовать панель выражений.zIndex =»n» и на самом деле работает, если я закодирую его в Powershell.
<Button Name="Cell2" Grid.Row="0" Grid.Column="1" Opacity="1" Background="#eae0f5" FontFamily="Impact" FontSize="25" Foreground="White" Content="UP" Panel.ZIndex="1"/>
<Button Name="Cell81" Grid.Row="0" Grid.Column="1" Opacity="1" Background="#dfd1f0" FontFamily="Impact" FontSize="25" Foreground="White" Content="DOWN" Panel.ZIndex="0"/>
Но когда я хочу вызвать его нажатием кнопки, например:
$WPFCell1.add_Click({$WPFCell81.Panel.ZIndex ="2"})
он возвращается со следующей ошибкой:
The property 'ZIndex' cannot be found on this object. Verify that the property exists and can be set.
At line:149 char:22
$WPFCell1.add_Click({$WPFCell81.Panel.ZIndex ="2"})
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidOperation: (:) [], RuntimeException
FullyQualifiedErrorId : PropertyNotFound
Куда мне следует добавить zIndex свойства, если это ошибка?
Я знаю, что, вероятно, есть разные способы настройки этого, но я хотел бы найти способ сделать это с помощью этого кода, я новичок, и я работаю с библиотекой, которая работает с таким кодом.
Большое спасибо всем вам!
Это весь код, который можно приостановить:
#MultiView click overlay
$inputXML = @"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="Window"
WindowStyle = "None" WindowState="Maximized"
ResizeMode = "NoResize" Title = "overlay" AllowsTransparency = "True" Background = "Transparent" Opacity = "1" Topmost = "True">
<Grid x:Name = "Grid" Background = "Transparent" ShowGridLines="False">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="96" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="96" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="55" />
<RowDefinition Height="190" />
<RowDefinition Height="190" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="55" />
</Grid.RowDefinitions>
<Button Name="Cell1" Grid.Row="0" Grid.Column="0" Opacity="1" Background="#eae0f5" FontFamily="Impact" FontSize="25" Foreground="White" Content="MENU"/>
<Button Name="Cell2" Grid.Row="0" Grid.Column="1" Opacity="1" Background="#dfd1f0" FontFamily="Impact" FontSize="25" Foreground="White" Content="UP" Panel.ZIndex ="1"/>
<Button Name="Cell3" Grid.Row="0" Grid.Column="2" Opacity="1" Background="#d4c1eb" FontFamily="Impact" FontSize="25" Foreground="White" Content="3"/>
<Button Name="Cell4" Grid.Row="0" Grid.Column="3" Opacity="1" Background="#c9b2e6" FontFamily="Impact" FontSize="25" Foreground="White" Content="4"/>
<Button Name="Cell5" Grid.Row="0" Grid.Column="4" Opacity="1" Background="#bfa3e1" FontFamily="Impact" FontSize="25" Foreground="White" Content="4"/>
<Button Name="Cell6" Grid.Row="0" Grid.Column="5" Opacity="1" Background="#b493dc" FontFamily="Impact" FontSize="25" Foreground="White" Content="5"/>
<Button Name="Cell7" Grid.Row="0" Grid.Column="6" Opacity="1" Background="#a984d7" FontFamily="Impact" FontSize="25" Foreground="White" Content="6"/>
<Button Name="Cell8" Grid.Row="0" Grid.Column="7" Opacity="1" Background="#9f74d2" FontFamily="Impact" FontSize="25" Foreground="White" Content="7"/>
<Button Name="Cell9" Grid.Row="0" Grid.Column="8" Opacity="1" Background="#9465cd" FontFamily="Impact" FontSize="25" Foreground="White" Content="8"/>
<Button Name="Cell10" Grid.Row="0" Grid.Column="9" Background="#f75590" FontFamily="Impact" FontSize="25" Foreground="White" Content="9" />
<Button Name="Cell11" Grid.Row="1" Grid.Column="0" Opacity="1" Background="#dfd1f0" FontFamily="Impact" FontSize="25" Foreground="White" Content="10"/>
<Button Name="Cell12" Grid.Row="1" Grid.Column="1" Opacity="0.01"/>
<Button Name="Cell13" Grid.Row="1" Grid.Column="2" Opacity="0.01"/>
<Button Name="Cell14" Grid.Row="1" Grid.Column="3" Opacity="0.01"/>
<Button Name="Cell15" Grid.Row="1" Grid.Column="4" Opacity="1" Background="#8956c8" FontFamily="Impact" FontSize="25" Foreground="White" Content="14"/>
<Button Name="Cell16" Grid.Row="1" Grid.Column="5" Opacity="1" Background="Yellow" FontFamily="Impact" FontSize="25" Foreground="White" Content="15"/>
<Button Name="Cell17" Grid.Row="1" Grid.Column="6" Opacity="0.01"/>
<Button Name="Cell18" Grid.Row="1" Grid.Column="7" Opacity="0.01"/>
<Button Name="Cell19" Grid.Row="1" Grid.Column="8" Opacity="0.01"/>
<Button Name="Cell20" Grid.Row="1" Grid.Column="9" Opacity="1" Background="#8956c8" FontFamily="Impact" FontSize="25" Foreground="White" Content="20"/>
<Button Name="Cell21" Grid.Row="2" Grid.Column="0" Opacity="1" Background="#cae916" FontFamily="Impact" FontSize="28" Foreground="White" Content="21"/>
<Button Name="Cell22" Grid.Row="2" Grid.Column="1" Opacity="0.01"/>
<Button Name="Cell23" Grid.Row="2" Grid.Column="2" Opacity="0.01"/>
<Button Name="Cell24" Grid.Row="2" Grid.Column="3" Opacity="0.01"/>
<Button Name="Cell25" Grid.Row="2" Grid.Column="4" Opacity="0.01"/>
<Button Name="Cell26" Grid.Row="2" Grid.Column="5" Opacity="0.01"/>
<Button Name="Cell27" Grid.Row="2" Grid.Column="6" Opacity="0.01"/>
<Button Name="Cell28" Grid.Row="2" Grid.Column="7" Opacity="0.01"/>
<Button Name="Cell29" Grid.Row="2" Grid.Column="8" Opacity="0.01"/>
<Button Name="Cell30" Grid.Row="2" Grid.Column="9" Opacity="1" Background="#cae916" FontFamily="Impact" FontSize="28" Foreground="White" Content="30"/>
<Button Name="Cell31" Grid.Row="3" Grid.Column="0" Opacity="1" Background="#c9b2e6" FontFamily="Impact" FontSize="30" Foreground="White" Content="31"/>
<Button Name="Cell32" Grid.Row="3" Grid.Column="1" Opacity="0.01"/>
<Button Name="Cell33" Grid.Row="3" Grid.Column="2" Opacity="0.01"/>
<Button Name="Cell34" Grid.Row="3" Grid.Column="3" Opacity="0.01"/>
<Button Name="Cell35" Grid.Row="3" Grid.Column="4" Opacity="0.01"/>
<Button Name="Cell36" Grid.Row="3" Grid.Column="5" Opacity="0.01"/>
<Button Name="Cell37" Grid.Row="3" Grid.Column="6" Opacity="0.01"/>
<Button Name="Cell38" Grid.Row="3" Grid.Column="7" Opacity="0.01"/>
<Button Name="Cell39" Grid.Row="3" Grid.Column="8" Opacity="0.01"/>
<Button Name="Cell40" Grid.Row="3" Grid.Column="9" Opacity="1" Background="#6a37a9" FontFamily="Impact" FontSize="30" Foreground="White" Content="40"/>
<Button Name="Cell41" Grid.Row="4" Grid.Column="0" Opacity="1" Background="#bfa3e1" FontFamily="Impact" FontSize="30" Foreground="White" Content="41"/>
<Button Name="Cell42" Grid.Row="4" Grid.Column="1" Opacity="0.01"/>
<Button Name="Cell43" Grid.Row="4" Grid.Column="2" Opacity="0.01"/>
<Button Name="Cell44" Grid.Row="4" Grid.Column="3" Opacity="0.01"/>
<Button Name="Cell45" Grid.Row="4" Grid.Column="4" Opacity="0.01"/>
<Button Name="Cell46" Grid.Row="4" Grid.Column="5" Opacity="0.01"/>
<Button Name="Cell47" Grid.Row="4" Grid.Column="6" Opacity="0.01"/>
<Button Name="Cell48" Grid.Row="4" Grid.Column="7" Opacity="0.25" Background="#0000ffff" FontFamily="Impact" FontSize="20" Foreground="White" Content="48"/>
<Button Name="Cell49" Grid.Row="4" Grid.Column="8" Opacity="0.25" Background="#0000ffff" FontFamily="Impact" FontSize="20" Foreground="White" Content="49"/>
<Button Name="Cell50" Grid.Row="4" Grid.Column="9" Opacity="1" Background="#61329a" FontFamily="Impact" FontSize="25" Foreground="White" Content="50"/>
<Button Name="Cell51" Grid.Row="5" Grid.Column="0" Opacity="1" Background="#b493dc" FontFamily="Impact" FontSize="30" Foreground="White" Content="51"/>
<Button Name="Cell52" Grid.Row="5" Grid.Column="1" Opacity="0.01"/>
<Button Name="Cell53" Grid.Row="5" Grid.Column="2" Opacity="0.01"/>
<Button Name="Cell54" Grid.Row="5" Grid.Column="3" Opacity="0.01"/>
<Button Name="Cell55" Grid.Row="5" Grid.Column="4" Opacity="0.01"/>
<Button Name="Cell56" Grid.Row="5" Grid.Column="5" Opacity="1" Background="#a984d7" FontFamily="Impact" FontSize="28" Foreground="White" Content="56"/>
<Button Name="Cell57" Grid.Row="5" Grid.Column="6" Opacity="1" Background="#9f74d2" FontFamily="Impact" FontSize="25" Foreground="White" Content="57"/>
<Button Name="Cell58" Grid.Row="5" Grid.Column="7" Opacity="1" Background="#cae916" FontFamily="Impact" FontSize="35" Foreground="White" Content="58"/>
<Button Name="Cell59" Grid.Row="5" Grid.Column="8" Opacity="1" Background="#f75590" FontFamily="Impact" FontSize="30" Foreground="White" Content="59"/>
<Button Name="Cell60" Grid.Row="5" Grid.Column="9" Opacity="1" Background="#4d287b" FontFamily="Impact" FontSize="20" Foreground="White" Content="60"/>
<Button Name="Cell61" Grid.Row="6" Grid.Column="0" Opacity="1" Background="#cae916" FontFamily="Impact" FontSize="60" Foreground="White" Content="61"/>
<Button Name="Cell62" Grid.Row="6" Grid.Column="1" Opacity="0.01"/>
<Button Name="Cell63" Grid.Row="6" Grid.Column="2" Opacity="0.01"/>
<Button Name="Cell64" Grid.Row="6" Grid.Column="3" Opacity="0.01"/>
<Button Name="Cell65" Grid.Row="6" Grid.Column="4" Opacity="0.01"/>
<Button Name="Cell66" Grid.Row="6" Grid.Column="5" Opacity="1" Background="#bfa3e1" FontFamily="Impact" FontSize="25" Foreground="White" Content="66"/>
<Button Name="Cell67" Grid.Row="6" Grid.Column="6" Opacity="1" Background="#a984d7" FontFamily="Impact" FontSize="25" Foreground="White" Content="67"/>
<Button Name="Cell68" Grid.Row="6" Grid.Column="7" Opacity="1" Background="#9465cd" FontFamily="Impact" FontSize="25" Foreground="White" Content="68"/>
<Button Name="Cell69" Grid.Row="6" Grid.Column="8" Opacity="1" Background="#a984d7" FontFamily="Impact" FontSize="30" Foreground="White" Content="69"/>
<Button Name="Cell70" Grid.Row="6" Grid.Column="9" Opacity="1" Background="#cae916" FontFamily="Impact" FontSize="45" Foreground="White" Content="70"/>
<Button Name="Cell71" Grid.Row="7" Grid.Column="0" Opacity="1" Background="#9f74d2" FontFamily="Impact" FontSize="25" Foreground="White" Content="71"/>
<Button Name="Cell72" Grid.Row="7" Grid.Column="1" Opacity="1" Background="#9465cd" FontFamily="Impact" FontSize="25" Foreground="White" Content="72"/>
<Button Name="Cell73" Grid.Row="7" Grid.Column="2" Opacity="1" Background="#8956c8" FontFamily="Impact" FontSize="25" Foreground="White" Content="73"/>
<Button Name="Cell74" Grid.Row="7" Grid.Column="3" Opacity="1" Background="#773dbd" FontFamily="Impact" FontSize="25" Foreground="White" Content="74"/>
<Button Name="Cell75" Grid.Row="7" Grid.Column="4" Opacity="1" Background="#6a37a9" FontFamily="Impact" FontSize="25" Foreground="White" Content="75"/>
<Button Name="Cell76" Grid.Row="7" Grid.Column="5" Opacity="1" Background="#61329a" FontFamily="Impact" FontSize="25" Foreground="White" Content="76"/>
<Button Name="Cell77" Grid.Row="7" Grid.Column="6" Opacity="1" Background="#4d287b" FontFamily="Impact" FontSize="25" Foreground="White" Content="77"/>
<Button Name="Cell78" Grid.Row="7" Grid.Column="7" Opacity="1" Background="#44236c" FontFamily="Impact" FontSize="25" Foreground="White" Content="78"/>
<Button Name="Cell79" Grid.Row="7" Grid.Column="8" Opacity="1" Background="#3a1e5c" FontFamily="Impact" FontSize="25" Foreground="White" Content="79"/>
<Button Name="Cell80" Grid.Row="7" Grid.Column="9" Opacity="1" Background="#30194d" FontFamily="Impact" FontSize="25" Foreground="White" Content="80"/>
<Button Name="Cell81" Grid.Row="0" Grid.Column="1" Opacity="1" Background="#8956c8" FontFamily="Impact" FontSize="25" Foreground="White" Content="DOWN" Panel.ZIndex ="0"/>
</Grid>
</Window>
"@
$inputXML = $inputXML -replace 'mc:Ignorable="d"','' -replace "x:N",'N' -replace '^<Win.*', '<Window'
[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
[xml]$XAML = $inputXML
#Read XAML
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
try{$Window=[Windows.Markup.XamlReader]::Load( $reader )}
catch [System.Management.Automation.MethodInvocationException] {
Write-Warning "We ran into a problem with the XAML code. Check the syntax for this control..."
write-host $error[0].Exception.Message -ForegroundColor Red
if ($error[0].Exception.Message -like "*button*"){
write-warning "Ensure your amp;<button in the `$inputXML does NOT have a Click=ButtonClick property. PS can't handle this`n`n`n`n"}
}
catch{#if it broke some other way :D
Write-Host "Unable to load Windows.Markup.XamlReader. Double-check syntax and ensure .net is installed."
}
# Create PowerShell object for each WPF button
$xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name "WPF$($_.Name)" -Value $Window.FindName($_.Name)}
# Map multiview window click to actual
function buttonclicked($btnNAme){
write-host $btnNAme
switch($btnNAme){
0{$Script:Window.close();break
}
}
}
$WPFCell1.add_Click({$WPFCell16.Panel.ZIndex ="1"})
# Turn on WPF overlay display
$Window.ShowDialog() | out-null
Ответ №1:
Только что нашел обходной путь для получения тех же результатов с помощью .Visibility «Скрытый» или «Видимый». Та же структура, то же использование Panel.Zindex=»n» но вместо изменения значения zIndex с помощью кнопки я скрываю или показываю слой сверху. Но я уверен, что есть более удобный способ действовать, поэтому, если у вас есть решение, пожалуйста, обратите внимание :).
Приветствую, M