Question about combo box and labels
I have a combo box that contains 5 items. I have one label on the window. What i would like to do is whenever a user clicks on one of the items in the combobox, text will populate that label. I would like to show information about whichever item is selected (IP address, etc.). Any help is appreciated. Thanks.
Code:
private void cmbGroups_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//Combo box selection changed. Re-bind data
string selected开发者_开发问答Group = (string)cmbGroups.SelectedItem;
BindGrid(selectedGroup);
}
Code:
private void BindGrid(string selectedGroup)
{
//Re-bind the grid
dgPortStatus.DataContext = _dicPortStatus[selectedGroup].Portstatus.DefaultView;
InitializeColumns();
}
Code:
private void _UpdatePortStatus()
{
string[] files = Directory.GetFiles(System.Configuration.ConfigurationSettings.AppSettings["portStatusDir"], "PortStatus.*");
foreach (string file in files)
{
PortStatus ps = new PortStatus();
ps.ReadXml(new StreamReader(file));
//ps.ReadXml(new FileStream(file, FileMode.Open, FileAccess.Read));
if (!_dicPortStatus.ContainsKey(ps.General[0].Group))
{
_dicPortStatus.Add(ps.General[0].Group, ps);
}
PortStatus psOrig = _dicPortStatus[ps.General[0].Group];
foreach (PortStatus.PortstatusRow psr in ps.Portstatus.Rows)
{
DataRow[] drs = psOrig.Portstatus.Select("PortNumber = '" + psr.PortNumber + "'");
if (drs.Length == 1)
{
DateTime curDt = DateTime.Parse(drs[0]["LastUpdateDateTimeUTC"].ToString());
DateTime newDt = psr.LastUpdateDateTimeUTC;
if (newDt > curDt)
{
drs[0]["LastUpdateDateTimeUTC"] = newDt;
}
}
else if (drs.Length == 0)
{
psOrig.Portstatus.ImportRow(psr);
}
else
{
throw new Exception("More than one of the same portnumber on PortStatus file: " + file);
}
}
}
foreach (string groupName in _dicPortStatus.Keys)
{
if (!cmbGroups.Items.Contains(groupName))
{
cmbGroups.Items.Add(groupName);
cmbGroups.SelectedItem = groupName;
}
}
Code:
private Dictionary<string, PortStatus> _dicPortStatus = new Dictionary<string, PortStatus>()
<ComboBox Name="ComboBox1">
...
<ComboBox />
<Label Text="{Binding ElementName=ComboBox1, Path=SelectedItem}" />
--EDIT--
Extended Example:
<StackPanel>
<ComboBox Name="ComboBox1"
DisplayMemberPath="FirstName"></ComboBox>
<StackPanel DataContext="{Binding ElementName=ComboBox1, Path=SelectedValue}">
<Label Content="{Binding FirstName}" />
<Label Content="{Binding LastName}" />
<Label Content="{Binding Age}" />
</StackPanel>
</StackPanel>
Code:
InitializeComponent();
ObservableCollection<Person> persons = new ObservableCollection<Person>() {
new Person(){ FirstName = "John", LastName = "Doe", Age = 25 },
new Person(){ FirstName = "John", LastName = "Smith", Age = 35 },
new Person(){ FirstName = "Susan", LastName = "Smith", Age = 31 },
new Person(){ FirstName = "Anthony", LastName = "Jones", Age = 31 },
};
ComboBox1.ItemsSource = persons;
Class:
public class Person
{
public String FirstName { get; set; }
public String LastName { get; set; }
public Int32 Age { get; set; }
}
--EDIT:2--
Create a new class:
class GroupInfo
{
public String GroupName { get; set; }
public String IP { get; set; }
}
And change your code to following:
foreach (string groupName in _dicPortStatus.Keys)
{
if (!cmbGroups.Items.Contains(groupName))
{
cmbGroups.Items.Add(new GroupInfo(){ GroupName = groupName, IP = <Write Code to get IP>);
cmbGroups.SelectedItem = groupName;
}
}
Change XAML to following:
<ComboBox Name="ComboBox1" DisplayMemberPath="GroupName">
...
<ComboBox />
<Label Text="{Binding ElementName=ComboBox1, Path=SelectedItem.IP}" />
in Win Forms:
comboBox.SelectedIndexChanged += onSelectedIndexChanged;
private void onSelectedIndexChanged(object sender, EventArgs e)
{
object item = comboBox.SelectedItem;
string text = //get text from item
label.Text = text;
}
Hi you can try something like this
private void ComboBox1_SelectedIndexChanged(object sender,
System.EventArgs e)
{
ComboBox comboBox = (ComboBox) sender;
string myItemText = (string) ComboBox1.SelectedItem;
// populate
MyTextBox.Text = myItemText;
}
精彩评论