簡體   English   中英

如何獲得ComboBox內容價值?

[英]How to get ComboBox content value?

我想從組合框中獲取內容。 我已經嘗試了一些方法來做到這一點,但是它不能正常工作。

這是我的組合框的示例:

<ComboBox x:Name="cmbSomething" Grid.Column="1" Grid.Row="5" HorizontalAlignment="Center" Margin="0 100 0 0" PlaceholderText="NothingToShow">
    <ComboBoxItem>First item</ComboBoxItem>
    <ComboBoxItem>Second item</ComboBoxItem>
</ComboBox>

單擊按鈕后,我想顯示組合框選擇的項目值。

string selectedcmb= cmbSomething.Items[cmbSomething.SelectedIndex].ToString();
await new Windows.UI.Popups.MessageDialog(selectedcmb, "Result").ShowAsync();

為什么此代碼不起作用? 我的結果不是顯示組合框內容,而是顯示以下文本:

Windows.UI.Xaml.Controls.ComboBoxItem

您需要ComboBoxItemContent屬性。 因此,這應該是您想要的:

var comboBoxItem = cmbSomething.Items[cmbSomething.SelectedIndex] as ComboBoxItem;
if (comboBoxItem != null)
{
    string selectedcmb = comboBoxItem.Content.ToString();
}

我已經提出了有關使用模型而不是直接進行UI代碼后台訪問的建議。 這些是必需的部分:

BaseViewModel.cs

我在工作項目中的許多視圖模型中都使用了此模型。 您可以從技術上直接在視圖模型中實現它,但是我喜歡對其進行集中化以供重用。

public abstract class BaseViewModel : INotifyPropertyChanged
{
  public event PropertyChangedEventHandler PropertyChanged;
  private Hashtable values = new Hashtable();

  protected void SetValue(string name, object value)
  {
     this.values[name] = value;
     OnPropertyChanged(name);
  }

  protected object GetValue(string name)
  {
     return this.values[name];
  }

  protected void OnPropertyChanged(string name)
  {
     PropertyChangedEventHandler handler = PropertyChanged;
     if (handler != null)
     {
        handler(this, new PropertyChangedEventArgs(name));
     }
  }
}

ComboViewModel.cs

您將綁定這些內容以使其易於獲取值。 我將其稱為ComboViewModel因為我只處理您的ComboBox。 您將需要一個更大的視圖模型並使用一個更好的名稱來處理所有數據綁定。

public class ComboViewModel : BaseViewModel
{

  public ComboViewModel()
  {
     Index = -1;
     Value = string.Empty;
     Items = null;
  }

  public int Index
  {
     get { return (int)GetValue("Index"); }
     set { SetValue("Index", value); }
  }
  public string Value
  {
     get { return (string)GetValue("Value"); }
     set { SetValue("Value", value); }
  }
  public List<string> Items
  {
     get { return (List<string>)GetValue("Items"); }
     set { SetValue("Items",value); }
  }
}

Window1.xaml

這只是我為演示/測試所做的。 注意各種綁定。

<Window x:Class="SO37147147.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
      <Grid.RowDefinitions>
         <RowDefinition Height="Auto" />
         <RowDefinition Height="*" />
         <RowDefinition Height="Auto" />
      </Grid.RowDefinitions>
      <Grid.ColumnDefinitions>
         <ColumnDefinition Width="*" />
         <ColumnDefinition Width="Auto" />
      </Grid.ColumnDefinitions>
      <ComboBox x:Name="cmbSomething" Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="0" HorizontalAlignment="Center" MinWidth="80"
                ItemsSource="{Binding Path=Items}" SelectedIndex="{Binding Path=Index}" SelectedValue="{Binding Path=Value}"></ComboBox>
      <TextBox x:Name="selectedItem" MinWidth="80" Grid.Row="2" Grid.Column="0" Text="{Binding Path=Value}" />
      <Button x:Name="displaySelected" MinWidth="40" Grid.Row="2" Grid.Column="1" Content="Display" Click="displaySelected_Click" />
   </Grid>
</Window>

Window1.xaml.cs

這是背后的代碼。 沒什么大不了的! 一切都通過dataContext實例進行訪問。 無需知道控件名稱等。

public partial class Window1 : Window
{
  ComboViewModel dataContext = new ComboViewModel();

  public Window1()
  {
     InitializeComponent();
     dataContext.Items=new List<string>(new string[]{"First Item","Second Item"});
     this.DataContext = dataContext;
  }

  private void displaySelected_Click(object sender, RoutedEventArgs e)
  {
     MessageBox.Show(String.Format("Selected item:\n\nIndex: {0}\nValue: {1}", dataContext.Index, dataContext.Value));
  }
}

您可以添加用於從數據庫填充模型,將更改保存到數據庫等的業務邏輯。當您更改視圖模型的屬性時,UI將自動更新。

暫無
暫無

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

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