簡體   English   中英

C#UWP如何獲取更改后的ComboBoxItem的值

[英]C# UWP how to get the value of changed ComboBoxItem

在我的.xaml文件中,我的組合框如下:

<ComboBox Name="CLengthCombo" SelectionChanged="ComboBox_SelectionChanged"> <ComboBoxItem Content="24"/> <ComboBoxItem Content="25"/> <ComboBoxItem Content="26" IsSelected="True"/> <ComboBoxItem Content="27"/> </ComboBox>如何實現ComboBox_SelectionChanged事件,以便獲得應用程序運行時用戶更改的comboBoxItem的內容? 在這種情況下,即使使用SelectionChanged事件也是正確的嗎? 以下內容不起作用:

private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { string chosenItem = CLengthCombo.PlaceholderText; } private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { string chosenItem = CLengthCombo.PlaceholderText; }預先感謝您的幫助!

你可以像下面這樣

private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var comboBoxItem = e.AddedItems[0] as ComboBoxItem;
            if (comboBoxItem == null) return;
            var content = comboBoxItem.Content as string;
            if (content != null && content.Equals("some text"))
            {
                //do what ever you want
            }
        }

您可以使用組合框的SelectedItem屬性

(CLengthCombo.SelectedItem as ComboBoxItem).Content

https://msdn.microsoft.com/library/windows/apps/windows.ui.xaml.controls.combobox.aspx#properties

獲取您的組合框以使其正常工作,c。(...)具有SelectedItem,SelectedText ...:

private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var c = sender as ComboBox;

    var item = c.(...);
}

暫無
暫無

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

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