0

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?

VBScab
  • 13
  • 3
  • Can you add an example to your question? It sounds like you **do** want to set the visibility. Are you looking for something like the Expander control? – Cpt.Whale Apr 04 '22 at 17:28
  • I have no example, since I don't yet know which control would be best. Maybe a fulller explanation will help. I have 3 scenarios for the user to choose from, options for which are listed in a drop-down. There is a default setting which displays a number of check-boxes, another drop-down and some text-boxes. 1/2 – VBScab Apr 05 '22 at 08:03
  • When the user selects the 2nd option from the drop-down, the default controls are swapped out for a different set of check-boxes, a single drop-down and a date-picker. The third selection swaps out the above and draws a further different set of check-boxes, 3 drop-downs and a text-box. As I mentioned, hiding/revealing those individually will produce a spaghetti of code that I want to avoid. 2/2 – VBScab Apr 05 '22 at 08:06
  • Each control has a Visibility property that you can set to hidden/visible. – RetiredGeek Apr 05 '22 at 12:48
  • @VBScab Maybe you just want a `ContentControl` like the answer here talks about: https://stackoverflow.com/a/2130193/7411885 . Add your other controls to it and toggle its visibility. You could also use more `Canvas` tags or the other types of `Panel`, depending on how you want to organize things. It will be easier to build a proof of concept and see whether they work for yourself – Cpt.Whale Apr 05 '22 at 20:17

2 Answers2

0

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.

VBScab
  • 13
  • 3
0

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.

VBScab
  • 13
  • 3
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 07 '22 at 06:47