List<Member>Members=new List<Member>();
private void BtnRegisterAcct_Click (object sender, RoutedEventArgse)
{
string cellphone=txtMobileNr.Text;
string firstName = txtFirstName.Text;
string lastName = txtLastName.Text;
string address = txtAddress.Text;
if(!string.IsNullOrEmpty(firstName)&!string.IsNullOrEmpty(lastName)&!string.IsNullOrEmpty(cellphone)&string.IsNullOrEmpty(address))
{
if(ComboBox.SelectedItemProperty!=null)
{
members.Add (new Member (cellphone, firstName, lastName, address, I want to put any ComboBox item here);
}
}
}
In the if statement, we condition that all the variables we initially substituted are not IsNullOrEmpty, and that the value of the combo box in the nested if statement is also not null.
Also, if that is the case, I would like to add the variable I mentioned earlier to the list members and any item selected in the combo box.However, I don't know how to handle the items in the combo box in the code.
The ComboBox items are Child member, Normal member, and VIP member.What should I do?Thank you for your cooperation.
c# visual-studio wpf
You can retrieve the ComboBoxItem
you are selecting in the combo box SelectedItem
.
From there, you can extract the value displayed in the item by taking out the Content
and making it a string type.
ComboBox.SelectedItemProperty
is a static property, so you cannot actually take the selected item from the combo box instance described on xaml.
You can use the code below to display the combo box values in the list box.(Exclude text boxes such as address and name)
If you have configured your own class for ItemsSource
in the combo box, cast SelectedItem
.
MainWindow.xaml
<Window:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns: x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns: mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc —Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<StackPanel>
<ComboBox Name="CmbMember">
<ComboBoxItem Content="Child member"/>
<ComboBoxItem Content="Normal member"/>
<ComboBoxItem Content="Vip member"/>
</ComboBox>
<Button Name="BtnRegisterAcct" Content="test"/>
<ListBox Name="LstCustomer" DisplayMemberPath="DisplayMember"/>
</ StackPanel>
</Grid>
</Window>
MainWindow.xaml.cs
using System.Collections.General;
using System.Windows;
using System.Windows.Controls;
namespace WpfApp1
{
/// <summary>
/// MainWindow.xaml Interaction Logic
/// </summary>
public partial class MainWindow—Window
{
public List <Customer>Customers {get;set;}
public MainWindow()
{
InitializeComponent();
Customers = new List <Customer>();
LstCustomer.ItemsSource=Customers;
BtnRegisterAcct.Click+=BtnRegisterAcct_Click;
}
private void BtnRegisterAcct_Click (object sender, RoutedEventArgse)
{
string cellphone="090-1234-5678";
string firstName = "John";
string lastName = "Smith";
string address = "Japan";
if(!string.IsNullOrEmpty(firstName)&!string.IsNullOrEmpty(lastName)&!string.IsNullOrEmpty(cellphone)&amp;!string.IsNullOrEmpty(address))
{
if(CmbMember.SelectedItem!=null)
{
varitem=(ComboBoxItem)CmbMember.SelectedItem;
// When you want to retain the combo box item you are selecting.
Customers.Add (new Customer (cellphone, firstName, lastName, address, item));
// When you want to take out the string inside,
Customers.Add(new Customer(cellphone, firstName, lastName, address, item.Content.ToString()));
// redrawing
LstCustomer.Items.Refresh();
}
}
}
}
public class customer
{
public Customer (string cellphone, string firstName, string lastName, string address, string member)
{
CellPhone= cellphone;
FirstName = firstName;
LastName = lastName;
Address = address;
Member = member;
}
public Customer (string cellphone, string firstName, string lastName, string address, ComboBoxItem)
: This(cellphone, firstName, lastName, address, item.Content.ToString())
{
}
public string CellPhone {get;set;}
public string FirstName {get;set;}
public string LastName {get;set;}
public string address {get;set;}
public string member {get;set;}
public string DisplayMember
{
get
{
return string.Format("[{0}]{1}{2}:{3}({4}), Member, FirstName, LastName, Address, CellPhone);
}
}
}
}
911 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
574 Who developed the "avformat-59.dll" that comes with FFmpeg?
572 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
617 Uncaught (inpromise) Error on Electron: An object could not be cloned
© 2024 OneMinuteCode. All rights reserved.