C#文本框显示对象名称而不是值

时间:2018-12-21 14:17:08

标签: c# wpf textbox listbox listboxitem

我有一个用c#编写的表单,带有各种下拉列表,但是表单上的列表框存在问题。双击它们时,我需要使用从列表框中选择的值来填充文本框。我已经完成了click事件,但是文本框只会填充对象名称,而不会填充列表框的值

  

'System.Windows.Controls.SelectedItemCollection'

代替实际值。

这是我正在处理的整个代码块:

我应该在一开始就做到这一点-这是我正在处理的完整代码块:

else if (theValue.FieldName.Equals("UIPathList", StringComparison.OrdinalIgnoreCase) == true)
            {
                int nRow = 14;
                Button theUIPathOptionsButton = new Button();

                TextBox theOldValueTextBox = AddLabelAndOldValue(theHelper, nRow, theValue);
                theOldValueTextBox.Text = theValue.OldValue.Replace(",", "," + Environment.NewLine);

                theUIPathOuterStackPanel = new StackPanel
                {
                    Visibility = Visibility.Visible,
                    Orientation = Orientation.Vertical,
                    Background = new SolidColorBrush(Colors.White),
                    ClipToBounds = true,
                };

                theUIPathOptionsInnerStackPanel = new StackPanel
                {
                    Visibility = Visibility.Visible,
                    Orientation = Orientation.Horizontal,
                    Background = new SolidColorBrush(Colors.White)
                };
                theUIPathOuterStackPanel.ClipToBounds = true;

                TextBox theNewTextBox = new TextBox
                {
                    TabIndex = nRow,
                    TextWrapping = TextWrapping.Wrap,
                    AcceptsReturn = true,
                };

                theNewTextBox.Clear();
                theNewTextBox.MouseDoubleClick += MultiLineChildDatapointList_HandleMouseDoubleClick;
                theNewTextBox.Focusable = true;
                theNewTextBox.HorizontalAlignment = HorizontalAlignment.Stretch;
                theNewTextBox.Width = 365;

                theNewTextBox.PreviewKeyDown += theGetMetadataHelper.Preview_KeyDown_IsMultilineText;

                theNewTextBox.Tag = theValue;

                ListBox theUIPathOptionslistBox = new ListBox();
                theUIPathOptionslistBox.Items.Add("RuntimeDefaults");
                theUIPathOptionslistBox.Items.Add("CommonSettings");
                theUIPathOptionslistBox.Items.Add(InputDatapointManager.CONST_CHANGE_RECORD_CHANGES_CLEAR_VALUE);
                theUIPathOptionslistBox.TabIndex = nRow;
                theUIPathOptionslistBox.SelectionMode = SelectionMode.Multiple;
                theUIPathOptionslistBox.ClipToBounds = true;
                theUIPathOptionslistBox.Focusable = true;
                theUIPathOptionslistBox.Visibility = Visibility.Hidden;
                theUIPathOptionslistBox.Height = 34;

                theUIPathOptionsInnerStackPanel.Children.Add(theNewTextBox);
                theUIPathOptionsInnerStackPanel.Children.Add(theUIPathOptionsButton);

                theUIPathOuterStackPanel.Children.Add(theUIPathOptionsInnerStackPanel);
                theUIPathOuterStackPanel.Children.Add(theUIPathOptionslistBox);

                void button1_click(object sender, EventArgs e)
                {
                    theUIPathOptionslistBox.Visibility = Visibility.Visible;
                }

                void button1_doubleclick(object sender, EventArgs e)
                {
                    theNewTextBox.Text = theUIPathOptionslistBox.SelectedItem.ToString();
                }

                theUIPathOptionsButton.Click += button1_click;
                theUIPathOptionslistBox.MouseDoubleClick += button1_doubleclick;

                Grid.SetColumn(theUIPathOuterStackPanel, 4);
                Grid.SetRow(theUIPathOuterStackPanel, nRow);
                theDataGrid.Children.Add(theUIPathOuterStackPanel);

                theEditControlList.Add(theNewTextBox);
            }

4 个答案:

答案 0 :(得分:1)

(可能)已经在这里回答:Getting value of selected item in list box as string

13     ingress.gcp.kubernetes.io/pre-shared-cert: "staging-google-managed-ssl"

然后,您只需将项目添加到文本框中:

Rectangle {
    id: rect

    property bool left : true

    states: [
        State {
            name: "left"
            when: rect.left
            PropertyChanges {
                target: rect
                anchors.left: parent.right
            }
        },
        State {
            name: "right"
            when: !rect.left
            PropertyChanges {
                target: rect
                anchors.right: parent.left    //note switched with above ;-)
            }
        }
    ]
}

如果您不想创建新的字符串变量,那么该变量也可以工作:

string myItem = listBox1.GetItemText(listBox1.SelectedItem);

答案 1 :(得分:0)

这是因为TextBox将在其绑定的对象上使用ToString()方法,默认情况下将返回类名。

您的解决方案是重写类的ToString()方法以返回所需的值,或者将TextBox的text属性设置为所需的文本值,而不是对象。

答案 2 :(得分:0)

ListBox,Item是对象而不是字符串的集合,因此您必须让它知道如何将其转换为字符串,否则它将使用其默认的.ToString()函数,显然您项目中的当前对象没有给出理想的结果。

想象一下,物品属于以下类别:

class SomeClass
{
      public int Id;
      public string Name;
}

您可以执行以下三个操作之一:

1。将ListBox的DisplayMember设置为Name

2.add覆盖类的方法,以便它覆盖其.ToString()并返回其Name属性:

class SomeClass
{
      public int Id;
      public string Name;

      public override string ToString()
      {
            return Name;
      }
}

3。只需将其转换为实型并获取所需的属性即可:

SomeClass selected = (SomeClass)ListBox.SelectedItem;
TextBox1.Text = selected.Name;

答案 3 :(得分:0)

我最终得到的解决方案如下:

//This will select Year first
WebElement year = driver.findElement(Year);
Actions Action4 = new Actions(driver);
Action4.moveToElement(year).click().perform();
Thread.sleep(2000);
//This will select the month
WebElement month = driver.findElement(Month);
Actions Action5 = new Actions(driver);
Action5.moveToElement(month).click().perform();
Thread.sleep(2000);
//This will select the Date
WebElement anydate = driver.findElement(Date);
Actions Action6 = new Actions(driver);
Action6.moveToElement(anydate).click().perform();
Thread.sleep(2000);
相关问题