簡體   English   中英

更新 WPF XAML Combobox selecteditem from powershell

[英]Updating WPF XAML Combobox selecteditem from powershell

我只是簡單地嘗試更改 combobox 從 powershell 下拉菜單中選擇的項目。每當我使用:

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

我收到錯誤:

“...無法在此 object 上找到。驗證該屬性是否存在並且可以設置。”

這是我的 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>

我不確定我在這里遺漏了什么......看起來這應該非常容易。 System.Windows.Forms 使這變得輕而易舉。

編輯 - 添加 powershell 端:

$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

我今天早上讀到你必須使用一個 observablecollection 才能通知 UI 更改和刷新......但我仍然沒有任何運氣。 我只使用一個線程,所以也許這就是問題所在?

我試圖用另一個按鈕單擊事件更改組合框的下拉選擇。

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

但正如我所說,當我單擊 UI 上的按鈕時,沒有任何變化,選擇保持不變。

您應該設置綁定到ComboBoxSelectedItem屬性的cbox_DiskD源屬性:

$dg_Servers.Items[0].cbox_DiskD = 88

可能不是正確的方法,但在這一點上我花了太多時間試圖找出更好的方法:)

我必須綁定到我的 class 中的不同元素。使它們不同可以讓我將所有項目保留在下拉列表中,而 select 是一個特定的項目。 否則,如果我保持它們不變,我所有的項目都會消失,並且設置為 88。(注意:cbox_DiskC 作為項目源,cbox_DiskC_selected 作為下面的選定項目)

                <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>

一旦我這樣做了,在 add_click 中我基本上 $nulled itemsource,並重新添加它強制我的 GUI 刷新。

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

稍后我將制作一個 function 來進行刷新以稍微清理一下。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM