2017-10-15 83 views
0

我正在尋找的最終效果是「消失」行「1」,又名:從頂部開始的第二行,以便Text1向下擴展到區域中如果第1行不存在。Powershell WPF XAML摺疊RowDefinition高度或設置爲零的事件

這是我試圖完成的模型。前2行和列幾乎完全相同。

如果我將此XAML插入到VS2017社區,並將行高設置爲0,則可以使用。谷歌搜索了幾天後,我還沒有想出一個解決方案。

我根本不知道C#。

平臺: 的Windows 7 //辣妹V5

不久將視窗10 //辣妹V5

理想情況下,我想開始使用它關閉,並打開它的事件。

[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework') 
[xml]$XAML = @' 
<Window 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="TabularGrid" Height="300" Width="300"> 
    <Grid Name="Grid"> 
       <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="*" /> 
         <ColumnDefinition Width="80" /> 
       </Grid.ColumnDefinitions> 
       <Grid.RowDefinitions> 
         <RowDefinition Height="*" /> 
         <RowDefinition Height="40" /> 
         <RowDefinition Height="30" /> 
         <RowDefinition Height="30" /> 
       </Grid.RowDefinitions> 
       <TextBox Name="Text1" Background="Silver" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" >Text1</TextBox> 
       <TextBox Name="Text2" Grid.Row="1" Grid.Column="0" >Text2</TextBox> 
       <Button Name="Enter" Grid.Row="1" Grid.Column="1" >Enter</Button> 
       <Button Name="Open" Grid.Row="2" Grid.ColumnSpan="2" >Open</Button> 
       <Button Name="Close" Grid.Row="3" Grid.ColumnSpan="2" >Close</Button> 
     </Grid> 
</Window> 
'@ 

$reader=(New-Object System.Xml.XmlNodeReader $xaml) 
$Form=[Windows.Markup.XamlReader]::Load($reader) 
$xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name "WPF$($_.Name)" -Value $Form.FindName($_.Name)} 

#=========================================================================== 
# Store Form Objects In PowerShell 
#=========================================================================== 

Function Get-FormVariables{ 
if ($global:ReadmeDisplay -ne $true){Write-host "If you need to reference this display again, run Get-FormVariables" -ForegroundColor Yellow;$global:ReadmeDisplay=$true} 
write-host "Found the following interactable elements from our form" -ForegroundColor Cyan 
get-variable WPF* 
} 

Get-FormVariables 


#=========================================================================== 
# Shows the form 
#=========================================================================== 


$WPFOpen.Add_Click({ 
$WPFGrid.RowDefinition[1].Visibility = $true 
# or 
$WPFGrid.RowDefinition[1].Height="40" 
}) 

$WPFClose.Add_Click({ 
$WPFGrid.RowDefinition[1].Visibility = $false 
# or 
$WPFGrid.RowDefinition[1].Height="0" 
}) 
#> 

$Form.ShowDialog() | out-null 

謝謝你的期待。

回答

1

這是因爲它不是$WPFGrid.RowDefinition而是$WPFGrid.RowDefinitions,最後是s

我刪除Visibility的方法,因爲它沒有與RowDefinitions實現並由<RowDefinition Height="0" />改變<RowDefinition Height="40" />給行隱形啓動窗口。

此代碼工作

[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework') 
[xml]$XAML = @' 
<Window 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="TabularGrid" Height="300" Width="300"> 
    <Grid Name="Grid"> 
       <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="*" /> 
         <ColumnDefinition Width="80" /> 
       </Grid.ColumnDefinitions> 
       <Grid.RowDefinitions> 
         <RowDefinition Height="*" /> 
         <RowDefinition Height="0" /> 
         <RowDefinition Height="30" /> 
         <RowDefinition Height="30" /> 
       </Grid.RowDefinitions> 
       <TextBox Name="Text1" Background="Silver" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" >Text1</TextBox> 
       <TextBox Name="Text2" Grid.Row="1" Grid.Column="0" >Text2</TextBox> 
       <Button Name="Enter" Grid.Row="1" Grid.Column="1" >Enter</Button> 
       <Button Name="Open" Grid.Row="2" Grid.ColumnSpan="2" >Open</Button> 
       <Button Name="Close" Grid.Row="3" Grid.ColumnSpan="2" >Close</Button> 
     </Grid> 
</Window> 
'@ 

$reader=(New-Object System.Xml.XmlNodeReader $xaml) 
$Form=[Windows.Markup.XamlReader]::Load($reader) 
$xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name "WPF$($_.Name)" -Value $Form.FindName($_.Name)} 

#=========================================================================== 
# Store Form Objects In PowerShell 
#=========================================================================== 

Function Get-FormVariables{ 
if ($global:ReadmeDisplay -ne $true){Write-host "If you need to reference this display again, run Get-FormVariables" -ForegroundColor Yellow;$global:ReadmeDisplay=$true} 
write-host "Found the following interactable elements from our form" -ForegroundColor Cyan 
get-variable WPF* 
} 

Get-FormVariables 


#=========================================================================== 
# Shows the form 
#=========================================================================== 


$WPFOpen.Add_Click({ 
    $WPFGrid.RowDefinitions[1].Height= 40 
}) 

$WPFClose.Add_Click({ 
    $WPFGrid.RowDefinitions[1].Height= 0 
}) 

$Form.ShowDialog() | out-null 
+1

謝謝馬努!我「發誓」我試過這個。 (顯然不是)。 – Steve

相關問題