wpf 中ListBox的用法

2021-09-08 16:37:22 字數 3846 閱讀 8266

listbox可以用來顯示類似web中新聞列表的樣式,顯示也比較靈活,可以自定定義模板

<

datatemplate

x:key

="listboxtemplate"

>

<

stackpanel

margin

="4"

>

<

dockpanel

>

<

grid

>

<

grid.rowdefinitions

>

<

rowdefinition

/>

<

rowdefinition

height

="2"

/>

grid.rowdefinitions

>

<

grid.columndefinitions

>

<

columndefinition

/>

<

columndefinition

width

="500"

/>

<

columndefinition

/>

grid.columndefinitions

>

<

image

height

="19"

source

="/financial;component/images/main/新聞列表圖示.png"

grid.row

="0"

grid.column

="0"

name

="image1"

stretch

="fill"

verticalalignment

="top"

width

="19"

/>

<

textblock

text=""

grid.row

="0"

grid.column

="1"

foreground

="white"

/>

<

textblock

text=""

grid.row

="0"

grid.column

="2"

foreground

="white"

/>

<

image

height

="2"

source

="/financial;component/images/main/新聞列表下劃線.png"

grid.row

="1"

grid.columnspan

="3"

margin

="0,0"

name

="image2"

stretch

="fill"

verticalalignment

="top"

width

="710"

/>

grid

>

dockpanel

>

stackpanel

>

datatemplate

>

usercontrol.resources

>

<

grid

name

="gdmainpanel"

width

="725"

height

="260"

margin

="5,0"

>

<

textblock

height

="23"

horizontalalignment

="left"

margin

="114,18,0,0"

name

="textblock1"

foreground

="#282c42"

text

verticalalignment

="top"

/>

<

listbox

margin

="0,49,0,34"

name

="listboxnews"

itemtemplate=""

borderthickness

="0"

previewmousedoubleclick

="listbox1_previewmousedoubleclick"

>

<

listbox.background

>

<

imagebrush

imagesource

="/financial;component/images/main/個人資料背景.png"

/>

listbox.background

>

<

listbox.resources

>

<

style

targettype

="listboxitem"

>

<

style.resources

>

<

solidcolorbrush

x:key=""

color

="#999999"

/>

<

solidcolorbrush

x:key=""

color

="gray"

/>

<

solidcolorbrush

x:key=""

color

="gray"

/>

<

solidcolorbrush

x:key=""

color

="gray"

/>

style.resources

>

style

>

listbox.resources

>

listbox

>

後台是通過泛型集合ilist《實體》來完成繫結

/// <

summary

>

/// 初始化資料

///

summary

>

private void initializedata()

pager.pagesize = 5;

webnewsservices.news newlist = webnews.isselectpaging(sqlstr.tostring(), pager.pagesize, pager.pagecurrent, out _datatotal);

ilist

<

webnewsservices.news

> list = new list<

webnewsservices.news

>

(newlist);

listboxnews.itemssource = list;

}private void listbox1_previewmousedoubleclick(object sender, mousebuttoneventargs e)

}

WPF中ListBox的繫結

wpf中列表式控制項派生自itemscontrol類,繼承了itemssource屬性。itemssource屬性可以接收乙個ienumerable介面派生類的例項作為自己的值 所有可被迭代遍歷的集合都實現了這個介面,如陣列 list等 每乙個 itemscontrol的派生類都有自己的條目容器,如...

WPF 拖拽ListBox中的Item

整理了兩個關於wpf拖拽listbox中的item的功能。專案位址 需求一 兩個listbox,拖拽其中乙個listbox的item,放置到另乙個listbox中。參考 右邊listbox2本來是空的,從左邊listbox1中拖拽了乙個item過去。需求二 單個listbox,拖拽item,釋放後i...

WPF 拖拽ListBox中的Item

整理了兩個關於wpf拖拽listbox中的item的功能。專案位址 需求一 兩個listbox,拖拽其中乙個listbox的item,放置到另乙個listbox中。參考 右邊listbox2本來是空的,從左邊listbox1中拖拽了乙個item過去。需求二 單個listbox,拖拽item,釋放後i...