如何得到Combobox被选中的内容

xinen8721 2012-02-09 04:52:37
我在WPF中用到了Combobox下拉菜单,当选中了一个选项我想要提取该选项的内容时,却出现了一下情况,该怎么解决,谢谢~

string Position = combobox1.SelectedValue.ToString();

Messagebox.Show("Position: "+combobox1.SelectedValue.ToString());

输出结果(注意Position 当前的内容时Guard):

Position: System.Windows.Controls.ComboBoxItem: Guard
...全文
248 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
宝_爸 2012-02-09
  • 打赏
  • 举报
回复
用SelectedItem 试一试。


ComboBoxItem lbi = combobox1.SelectedItem as ComboBoxItem ;
tb.Text = " You selected " + lbi.Content.ToString() + ".";
bdmh 2012-02-09
  • 打赏
  • 举报
回复
SelectedValuePath 属性指定用于确定 SelectedValue 属性值的属性路径

内容用MessageBox.Show(comboBox1.Text);
用法举例:从数据表中name字段写入VALComboBox的items,id字段写入VALComboBox的values,当从VALComboBox选择一个选项后,就可以从value属性获得相应的ID值,或者写value的值为某ID值,VALComboBox将定位在相应的选项上;unit VALComboBox;interfaceuses SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls, Forms, Menus, Dialogs, StdCtrls;type TValComboBox = class(TComboBox) private FValue: PString; FValues: TStrings; FOnChange: TNotifyEvent; function GetValue: string; function GetButtonValue(Index: Integer): string; procedure SetValue(const Value: string); procedure SetValues(Value: TStrings); protected procedure Change; dynamic; procedure Notification(AComponent: TComponent; Operation: TOperation); override; public constructor Create(AOwner: TComponent); override; destructor Destroy; override; property Value: string read GetValue write SetValue; property ItemIndex; published property Values: TStrings read FValues write SetValues; property OnChange: TNotifyEvent read FOnChange write FOnChange; end;procedure Register;implementationconstructor TValComboBox.Create(AOwner: TComponent);begin inherited Create(AOwner); FValue := NullStr; FValues := TStringList.Create; style := csDropDownList;end;destructor TValComboBox.Destroy;begin DisposeStr (FValue); FValues.Free; inherited Destroy;end;procedure TValComboBox.Notification(AComponent: TComponent; Operation: TOperation);begin inherited Notification(AComponent, Operation);end;function TValComboBox.GetValue : string;begin result:=values[itemindex];end;function TValComboBox.GetButtonValue(Index: Integer): string;begin if (Index < FValues.Count) and (FValues[Index] ‘‘) then Result := FValues[Index] else if (Index < Items.Count) then Result := Items[Index] else Result := ‘‘;end;procedure TValComboBox.SetValue (const Value: string);var I : Integer;begin AssignStr(FValue, Value); if (ItemIndex < 0) or (GetButtonValue(ItemIndex) Value) then begin if (ItemIndex >= 0) then ItemIndex := -1; for I := 0 to Items.Count - 1 do begin if GetButtonValue(I) = Value then begin ItemIndex := I; break; end

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧