Home > database >  In XAML, how best to duplicate the functionality of hiding an HTML <div>
In XAML, how best to duplicate the functionality of hiding an HTML <div>

Time:04-06

Work continues on migrating my HTA to a Powershell-driven, XAML-based form...

Parts of the HTA use the "STYLE='display:[none|blank];'" technique to show/hide parts of the dialog box when, for example, the user changes the selection in a drop-down box.

What is the simplest way to do this in XAML?

99% of the XAML I have is inside a 'Canvas' tag. Can I use more of those and set their visibility on/off? Obviously I could set each separate element's visibility but life's too short for that nonsense, right?

It looks like 'StackPanel' will do the job but does hiding that control hide all the controls encapsulated by it?

CodePudding user response:

I'm posting this as an answer so that I can format the text.

@RetiredGeek I believe I mentioned that I know I can set each control's visibility but my code is already getting spaghetti-like without an extra 90-odd lines enabling/disabling that property. –

@Cpt.Whale I just tried a ContentControl but because I'm loading the XAML from a file, I get this error:

WARNING: XML Load failed with error: Exception calling "Load" with "1" argument(s): "'Content' property has already been set on 'ContentControl'." Exception calling "Load" with "1" argument(s): "'Content' property has already been set on 'ContentControl'." At [path_to_my_project]\test.ps1:58 char:2 –

Here's how I'm loading the XAML:

$InputXAML_FullName = Convert-Path $ScriptDir\Form.xaml

# Load the file
$InputXAML = [IO.File]::ReadAllText($InputXAML_FullName)

#===========================================================================
# Remove stuff we don't want
#===========================================================================

$InputXAML = $InputXAML -replace 'mc:Ignorable="d"','' -replace "x:N",'N' -replace '^<Win.*', '<Window'

#===========================================================================
# Read the cleaned-up XAML
#===========================================================================

[xml]$xmlDoc = $InputXAML

$XAML_Reader=(New-Object System.Xml.XmlNodeReader $xmlDoc)

Try {
    $Form=[Windows.Markup.XamlReader]::Load($XAML_Reader)
}
Catch {
    Write-Warning "XML Load failed with error: $($Error[0])"
    Throw
}

I think I'm going to have to re-jig my grids and use a nested grid inside one of them.

CodePudding user response:

Nested grids have worked a treat! For now, there is a shed-load of duplicate code but I'll come to that when more of the core functionality is done.

Many thanks for all your pointers, folks! I appreciate it.

  • Related