Home > Mobile >  Updating WPF XAML Combobox selecteditem from powershell
Updating WPF XAML Combobox selecteditem from powershell

Time:10-06

I'm just simply trying to change the selected item on a combobox drop down from powershell. Whenever I use:

$dg_Servers.Items[0].cbox_Disk.SelectedItem = 88

I receive the error:

"...cannot be found on this object. Verify that the property exists and can be set."

Here's my XAML:

DataGridTemplateColumn Header="DiskC" Visibility="Visible" Width="61"  >
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                               <ComboBox
                               ItemsSource="{Binding Path=cbox_DiskC}"
                               SelectedItem="{Binding Path=cbox_DiskD, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
                            </ComboBox>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

I'm not sure what I'm missing here.. seems like this should be super easy. System.Windows.Forms makes this a no brainer to accomplish.

EDIT - adding the powershell side:

$row= New-Object NewRow -Property @{ServerName = ""; cbox_DiskC = (80..250); cbox_DiskD = (50..250); cbox_DiskE = (1..250); cbox_DiskF = (1..250); cbox_DiskG= (1..250); cbox_Mem=(1..16); cbox_CPU=(1..8); cbox_OSSpec = $Options_OSSpecs; cbox_Template = $Options_Templates; cbox_VLAN = $Options_VLAN ; cbox_Folder =  $Options_Folders }
$itemsource = New-Object -TypeName System.Collections.ObjectModel.ObservableCollection[object] -ArgumentList @($row)
$dg_Servers.ItemsSource = $itemsource

I read this morning that you have to use an observablecollection in order for the UI to be notified of a change and refresh.. but I'm still not having any luck. I'm only using one thread so maybe that's the issue?

I was trying to change the combobox's drop down selection with another buttons click event.

$btn_Validate.Add_Click({$dg_Servers.SelectedItems[0].cbox_DiskC = 88})

But as I said, when I click the button on the UI nothing changes and the selection remains the same.

CodePudding user response:

You should set the cbox_DiskD source property that is bound to the SelectedItem property of the ComboBox:

$dg_Servers.Items[0].cbox_DiskD = 88

CodePudding user response:

Probably not the right way to do it but at this point I've spent too much time trying to figure out a better way :)

I had to bind to a different element in my class. Making them different allows me to keep all my items in the dropdown AND select a specific one. Otherwise if I keep them the same, all of my items disappear and it's set to 88. (NOTE: cbox_DiskC as the itemsource and cbox_DiskC_selected as the selecteditem below)

                <DataGridTemplateColumn Header="DiskC" Visibility="Visible" Width="61" >
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                               <ComboBox                           
                               ItemsSource="{Binding Path = cbox_DiskC}"
                               SelectedItem="{Binding Path=cbox_DiskC_selected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
                            </ComboBox>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

Once I did that, in the add_click I basically $nulled the itemsource, and re-added it which forces my GUI to refresh.

$btn_Validate.Add_Click({$row.cbox_DiskC_selected = 88; $dg_Servers.ItemsSource = $null; $dg_Servers.ItemsSource = $itemsource})

Later on I'll make a function that does the refresh to clean it up a bit.

  • Related