使用onclick按钮在Powershell上的Xaml网格上重叠按钮



Powershell、Xaml和一般编码的新手。

多亏了StackOverflow,我在Powershell中构建了一个10列8行的网格,并用80个按钮填充(从1到80(。每个按钮都将触发一个操作。现在,我想有机会点击其中一个按钮,并将新按钮(或一个全新的网格(重叠在主网格上,以获得更多可用按钮(从81到160(。

举个明确的例子:让我们想象一下,我正在构建一个应用程序,以便在餐厅下单。网格1显示80个按钮。如果我按下";主菜";按钮,它将覆盖包含80个差异的80个按钮";主菜";。如果我按下";甜点"按钮等。

我理解使用Panel这个表达方式。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

如果这是错误,我应该在哪里添加Porperty ZIndex?我知道可能有不同的方法来设置这些东西,但我想找到一种方法,用这种代码再次实现它,我是一个傻瓜,我正在与一个处理这种代码的库合作。

非常感谢大家!

这是整个代码如果可以halp:

#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 &lt;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

刚刚找到了一个变通方法,可以获得相同的结果。可见性";隐藏的"或";可见";。相同的结构,相同的面板用途。Zindex=";n〃;但我没有用按钮修改ZIndex值,而是隐藏或显示顶部的层。但我相信还有一种更方便的行动方式,所以如果你有解决方案,请看:(。

干杯,M

最新更新