Note that there are some explanatory texts on larger screens.

plurals
  1. POLooping through controls in WPF / Powershell
    primarykey
    data
    text
    <p>I am trying to loop through some controls in a Powershell / WPF application. Nothing fancy, just setting some text on a mouseEnter event.</p> <p>It works fine if I don't loop the code:</p> <pre><code>$reader = New-Object System.Xml.XmlNodeReader $xaml $d = [Windows.Markup.XamlReader]::Load($reader) $d.FindName("GridOne").add_mouseEnter({ $d.FindName("HelpText").Content = "One" }) $d.FindName("GridTwo").add_mouseEnter({ $d.FindName("HelpText").Content = "Two" }) $d.FindName("GridThree").add_mouseEnter({ $d.FindName("HelpText").Content = "Three" }) $d.FindName("GridFour").add_mouseEnter({ $d.FindName("HelpText").Content = "Four" }) </code></pre> <p>But if I try the same thing as a loop it sets all the controls MouseEnter events to set the text to "Four", the last element of the array:</p> <pre><code>$reader = New-Object System.Xml.XmlNodeReader $xaml $d = [Windows.Markup.XamlReader]::Load($reader) $arrControls = ("One","Two","Three","Four") foreach ($control in $arrControls) { $d.FindName("Grid$control").add_mouseEnter({ $d.FindName("HelpText").Content = $control }) } </code></pre> <p>Anyone have any thoughts why this is, and how I can fix it?</p> <p>Thanks,</p> <p>Ben</p> <p>OK - This is even weirder...</p> <p>Tried to address this using a solution along the lines of Kent's suggestion. I got the same using $localControl, so thought I'd try using an array to ensure each entry was distinct:</p> <pre><code>$i = 0 $localControl = @() foreach ($control in $arrControls) { $localControl += $control write-host "$d.FindName('Grid$control').add_mouseEnter({ $d.FindName('HelpText').Content = $control })" $d.FindName("Grid$control").add_mouseEnter({ $d.FindName("HelpText").Content = $localControl[$i] $i = $i + 1 }) } </code></pre> <p>The behaviour I know get is that each time I mouseOver a control, the text just increments through the array one step. For example, the first control I hover over will output "One", the next will output "Two" and so on, until my array is exhausted when it just outputs null.</p> <p>This "One", "Two", "Three", "Four" output order is the same regardless of the order I hover over the controls.</p> <p>... Wait a minute. Have put the $i = $i + 1 in the MouseEnter!</p> <p>Amended to:</p> <pre><code>$i = 0 $localControl = @() foreach ($control in $arrControls) { $localControl += $control write-host "$d.FindName('Grid$control').add_mouseEnter({ $d.FindName('HelpText').Content = $control })" $d.FindName("Grid$control").add_mouseEnter({ $d.FindName("HelpText").Content = $localControl[$i] }) $i = $i + 1 } </code></pre> <p>Sets all the outputs to null.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload