wpf下拉框繫結怎麼讓他預設有選中項

2021-05-05 18:14:47 字數 2632 閱讀 1293

1樓:匿名使用者

繫結selecteditem屬性,如果需要後臺修改當前選擇屬性,lstselect需要使用依賴屬性或者繼承inotifypropertychanged訊息介面。

關於資料繫結,可以檢視博文

public partial class mainwindow : window

public string lstselectpublic mainwindow()

;lstselect = "name2";

this.datacontext = this;}}xaml

2樓:匿名使用者

一開始的時候,設定selectedindex = ***

或者設定selecteditem = ***

如果你的資料來源是在載入後動態繫結上的,建議自己做一個附加屬性來實現。例如:

public static class itemscontrolhelper

////// sets the autoselectedindex property.  this dependency property

/// indicates ....

///public static void setautoselectedindex(selector d, int value)

////// handles changes to the autoselectedindex property.

///private static void onautoselectedindexchanged(dependencyobject d, dependencypropertychangedeventargs e)

else

}private static void onitemschanged(object sender, itemschangedeventargs e)

}上面的**用了自己寫的一個庫,reflectgetproperty這個你自己用反射實現一下吧。

使用的時候

c#中怎麼獲取combobox下拉選單選中項的tag值,急。。。。。。

3樓:匿名使用者

按照你現在的做法,你儲存的tag,應該用一個符號對每個id進行分割,如:1,2,3

string arrid=combobox.tag.tostring().

split(new string ,system.stringsplitoptions.none);

string myid=arrid[combobox.selectindex];

這個myid就是你想要的id,前提是你的下拉框的值的順序要和id的順序一致,否則出錯。

另一個做法,把datatable繫結到combobox

cbodiscount.datasource = mydatatable;//這個datatable是你從資料庫拿出來的包括id和值

cbodiscount.valuemember = "id";//這對應你的欄位名

cbodiscount.displaymember = "name";對應你的欄位名

使用者選擇後,你可以通過這個語句,拿到id的值

string myid=cbodiscount.selectedvalue.tostring();

4樓:匿名使用者

我個人認為

當你使用tag值的時候 是因為你需要多個值儲存在combox列表裡面 只有text和value你不夠用

所以你選擇了tag值

但這是一種錯誤的程式設計方法

combox的item是object型別 當你給combox資料來源的時候 其實combox就已經把一個完整的型別做為item付給了combox 只不過是提供了text和value兩個比較容易操作的物件

我想你應該是用一個datatable當做資料來源了

那麼 當你選定一條記錄時 你想獲取這個combox的選中項的所有有關資訊時

((datarow)cbaccinfor.selecteditem)["table列名"].tostring();

你就能得到你想要的值

當然 前提是你給的資料型別是datatable 那每一個item的型別就是datarow

如果是ilist等做資料來源給combox 你就((資料行型別)cbaccinfor.selecteditem).類名.tostring()就行了

樓上寫comboboxitem,是沒有這個型別的 combox.selectitem是object的

5樓:匿名使用者

tag屬性是個基類物件型別object,所以你可一個把任何的物件例項賦值給它,所以如果你的zidian是個物件例項的話,你可以直接:

this.comleibie.tag = zidian;

然後在使用的時候,再把tag屬性強制型別轉換成zidian物件所對應的類例項就可以了。

6樓:4末午後紅茶

comboboxitem item=this.combobox1.selecteditem as comboboxite;

string tag=item.tag.tostring();

下拉框控制元件

這個可以做到.用組合框連線資料庫.能過組合框的選擇。啟用文字框等於資料庫地某個欄位.下拉框。form load if rec.state adstateopen then rec.close sql select from jbxx rec.open sql,con,adopenstatic,adl...

想做JS下拉框元件,怎麼讓彈出的下拉層處於最上層?z i

要讓z index起作用就得讓父元素position為absolute或relative.你是不是還用ie6呢啊。基本上z index都好用的了。如果ie6就比較麻煩了。要分層或者用iframe 你設定z index的大小別一樣 如何讓js做的下拉選單浮在最上層 jquery實現將div浮動到網頁最...

C winform下拉框成功後再追加分

你是第一個選擇之後然後根據第一個的值選中第二個下拉框的值嗎?不是的話照第一個寫就是了。如果是的話 我知道在asp.net中他的下拉框觸發事件是要滑鼠選取之後才會觸發的,如果是 讓他選中某個值的話,那就要手動的呼叫他的下拉框改變的事件,我大概寫一下,不一定正確 private void combox1...