簡體   English   中英

將組合框項目存儲為數組形式,並在WPF中檢索SelectedId

[英]Store Combobox items in The Form of Array and Retrieve SelectedId in WPF

我是c ++開發人員,最近轉到了這個令人驚嘆的WPF世界。 我正在開發WPF應用程序。 我的xaml文件中有一個組合框(BusRate)。

<ComboBox Height="23" ItemsSource="{Binding BusRateList}" SelectedIndex="2" Name="comboBox2" Width="85" />

我的Viewmodel類具有以下屬性:

public ObservableCollection<int> _busRate = new ObservableCollection<int>();
public ObservableCollection<int> BusRateList
    {
        get { return _busRate; }
        set
        {
            _busRate = value;
            NotifyPropertyChanged("BusRateList");
        }
    }

然后添加以下內容:

_busRate.Add(10);
_busRate.Add(50);
_busRate.Add(100);
_busRate.Add(200);
_busRate.Add(300);
_busRate.Add(400);
_busRate.Add(500);
_busRate.Add(600);      

這給了我組合框中的項目。 但是我想以包含所有這些值的數組的形式將這些項目添加到我的組合框中。 例如:

// C++ Code
static const char *busRate[8] = 
{
" 10", " 50", "100", "200", "300", "400", "500", "600"
};

因此,我可以對comboboxitemselected方法執行以下操作:

  • 從組合框獲取SelectedId並將其存儲在整數變量中。
  • 傳遞此整數變量。 演示如下:

     int id = comboBox->getSelectedId(); // C++ Code unsigned long speed = String(busRate[id-1]).getIntValue(); // C++ Code 

我如何在我的應用程序中實現這一點:)

您可以在類型為int的ViewModel中創建一個新屬性,並在一個將字符串轉換為int值的轉換器中創建一個新屬性。 現在,將新屬性綁定到ComboBox.SelectedItem-property:

<Window/UserControl/....Resouces>
    <local:YourConverter x:Key="TheConverter"/>
<Window/UserControl/....Resouces>

<ComboBox SelectedIten="{Binding Path=IntProp, Converter={StaticResource TheConverter}}"

編輯:

int id = Convert.ToInt32(BusRateList[comboBox.SelectedIndex]);

暫無
暫無

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

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