关于组合框和标签的问题

时间:2010-12-14 14:36:12

标签: c# wpf

我有一个包含5个项目的组合框。我在窗户上有一个标签。我想做的是当用户点击组合框中的一个项目时,文本将填充该标签。我想显示有关选择的项目(IP地址等)的信息。任何帮助表示赞赏。感谢。

代码:

private void cmbGroups_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    //Combo box selection changed. Re-bind data 
    string selectedGroup = (string)cmbGroups.SelectedItem; 
    BindGrid(selectedGroup); 
} 

代码:

    private void BindGrid(string selectedGroup)
    {
        //Re-bind the grid
        dgPortStatus.DataContext = _dicPortStatus[selectedGroup].Portstatus.DefaultView;

        InitializeColumns();
    }

代码:

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;
            }
        }

代码:

private Dictionary<string, PortStatus> _dicPortStatus = new Dictionary<string, PortStatus>()

3 个答案:

答案 0 :(得分:2)

在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;
}

答案 1 :(得分:2)

<ComboBox Name="ComboBox1">
...
<ComboBox />

<Label Text="{Binding ElementName=ComboBox1, Path=SelectedItem}" />

- 编辑 -

扩展示例

    <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>

代码:

        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;

类别:

public class Person
{
    public String FirstName { get; set; }
    public String LastName { get; set; }
    public Int32 Age { get; set; }
}

- 编辑:2 -

创建一个新类:

class GroupInfo
{
    public String GroupName { get; set; }
    public String IP { get; set; }
}

将代码更改为以下内容:

    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;
        }
    }

将XAML更改为以下内容:

<ComboBox Name="ComboBox1" DisplayMemberPath="GroupName">
    ...
<ComboBox />

<Label Text="{Binding ElementName=ComboBox1, Path=SelectedItem.IP}" />

答案 2 :(得分:-1)

您好,您可以尝试这样的事情

private void ComboBox1_SelectedIndexChanged(object sender, 
    System.EventArgs e)
{

    ComboBox comboBox = (ComboBox) sender;

    string myItemText = (string) ComboBox1.SelectedItem;

    // populate
    MyTextBox.Text = myItemText;

}