【问题标题】:Powershell WPF XAML Collapse RowDefinition Height or set to Zero with eventPowershell WPF XAML 折叠 RowDefinition 高度或使用事件设置为零
【发布时间】:2017-10-15 18:54:43
【问题描述】:

我正在寻找的最终效果是“消失”第“1”行,也就是:从顶部算起的第二行,以便 Text1 向下扩展到该区域,就好像第 1 行不存在一样。

这是我要完成的工作的模型。前 2 行和前列几乎完全相同。

如果我将此 XAML 插入 VS2017 社区并将行高设置为 0,则可以。谷歌搜索了几天后,我没有想出解决方案。

我根本不懂 C#。

平台: Windows 7 // Posh v5

即将推出 Windows 10 // Posh 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

感谢您的关注。

【问题讨论】:

    标签: wpf powershell xaml events rowdefinition


    【解决方案1】:

    那是因为它不是$WPFGrid.RowDefinition,而是$WPFGrid.RowDefinitions,末尾带有s

    我删除了Visibility 方法,因为它没有用RowDefinitions 实现,并由&lt;RowDefinition Height="0" /&gt; 更改&lt;RowDefinition Height="40" /&gt; 以启动不可见行的窗口。

    此代码正在运行

    [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
    

    【讨论】:

    • 谢谢马努!我“发誓”我试过这个。 (显然不是)。
    猜你喜欢
    • 1970-01-01
    • 2011-12-06
    • 2016-02-05
    • 2016-06-01
    • 1970-01-01
    • 2015-02-13
    • 1970-01-01
    • 2018-08-21
    • 2018-10-24
    相关资源
    最近更新 更多