简体   繁体   English

如何将ComboBox.SelectedItem转换为字符串

[英]How to convert ComboBox.SelectedItem to string

When saving to a database, the field referring to the text of a ComboBox is saved as Windows.UI.Xaml.ComboBoxItem . 保存到数据库时,引用ComboBox文本的字段另存为Windows.UI.Xaml.ComboBoxItem

What is the correct way to convert and save the text referring to the selected item in the ComboBox? 在ComboBox中转换和保存引用所选项目的文本的正确方法是什么?

ComboBox in XAML: XAML中的ComboBox:

<ComboBox
    x:Name="TipoComboBox"
    Width="300"
    Margin="{StaticResource SmallTopMargin}"
    Header="Bill Type"
    PlaceholderText="Select if it's payable or receivable bill"
    RelativePanel.Below="DescricaoTextBox">
        <ComboBoxItem x:Name="APagarComboBox" Content="Bill Payable" />
        <ComboBoxItem x:Name="AReceberComboBox" Content="Bill Receivable" />
</ComboBox>

Code behind: 后面的代码:

var conta = new Conta
{
    Vencimento = Convert.ToString(this.VencimentoDatePicker.Date.ToString("dd/MM/yyyy")),
    Descricao = this.DescricaoTextBox.Text,
    Tipo = this.TipoComboBox.SelectedItem.ToString(), // Windows.UI.Xaml.ComboBoxItem
    Categoria = this.CategoriaTextBox.Text,
    Valor = this.ValorTextBox.Text,
};

Please advise how to fix this. 请告知如何解决此问题。

EDIT 编辑

I've matched @Joelius' comments with @MK.DEVELOPER's answer, and obtained this line of code to solve my problem: 我已经将@Joelius的注释与@ MK.DEVELOPER的答案相匹配,并获得了以下代码行来解决我的问题:

Tipo = (this.TipoComboBox.SelectedItem as ComboBoxItem).Content.ToString()

sir you want to convert combobox.selecteditem to comboboxitem example : 先生,您想将combobox.selecteditem转换为comboboxitem示例:

public string GetSelectedComboboxText()
{
  // replace comboboxname to combobox control name
  var item = comboboxname.SelectedItem as ComboboxItem;
      return item.Content.ToString();
}

any quastion i am here 我在这里有什么疑问

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM