簡體   English   中英

使用 onclick 按鈕在 Powershell 上的 Xaml 網格上重疊按鈕

[英]Overlap Buttons on a Xaml Grid on Powershell with onclick button

Powershell、Xaml 和一般編碼的新手。

感謝 StackOverflow,我在 Powershell 中構建了一個具有 10 列和 8 行的網格,並用 80 個按鈕(從 1 到 80)填充它。 每個按鈕都會觸發一個動作。 現在,我想有機會單擊這些按鈕中的 1 個並在主要網格頂部重疊新按鈕(或一個全新的網格),以便有更多可用的按鈕(從 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

如果這是錯誤,我應該在哪里添加屬性 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 &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

剛剛找到了一種解決方法,可以使用 .Visibility "Hidden" 或 "Visible" 獲得相同的結果。 相同的結構,相同的 Panel.Zindex="n" 用法,但我沒有使用按鈕修改 ZIndex 值,而是隱藏或顯示頂部的圖層。 但我相信有一種更方便的行動方式,所以如果你有解決方案,pleaseeee :)。

干杯,M

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM