ItemsControl 解析
?先上個示例
<ItemsControl Margin="10" ItemsSource="{Binding}" Name="itemsControl">
<ItemsControl.Template><ControlTemplate TargetType="{x:Type ItemsControl}"><Border CornerRadius="5"><ScrollViewer VerticalScrollBarVisibility="Auto" ><ItemsPresenter /></ScrollViewer></Border></ControlTemplate></ItemsControl.Template><ItemsControl.ItemsPanel><ItemsPanelTemplate><WrapPanel Name="wp" /></ItemsPanelTemplate></ItemsControl.ItemsPanel><ItemsControl.ItemTemplate><DataTemplate><Image Source="{Binding XPath=@}" /></DataTemplate></ItemsControl.ItemTemplate></ItemsControl>
ItemsControl 是一種數(shù)據(jù)展示控件,大致分為三個部分組成:Template, ItemTemplate, ?ItemsPanel.
?
- 先說 Template ,Template 是整個控件的架構(gòu)設(shè)計,最外面放什么的東東,里面放什么東東,都在這里控制,負(fù)責(zé)宏觀的結(jié)構(gòu),下面這個例子:最外面是一個border,然后要放一個ScrollViewer用來滾動展示,滾動的內(nèi)容,就由ItemsPresenter 來決定。對應(yīng)blend菜單:右鍵—>Edit Template
? ???<ItemsControl.Template>
??????????? <ControlTemplate ?TargetType="{x:Type ItemsControl}">
??????????????? <Border CornerRadius="5">
??????????????????? <ScrollViewer VerticalScrollBarVisibility="Auto" >
??????????????????????? <ItemsPresenter? ?/>
??????????????????? </ScrollViewer>
??????????????? </Border>
??????????? </ControlTemplate>
??????? </ItemsControl.Template>
2. 再說ItemsPanel,作為數(shù)據(jù)展示,總要有個容器吧,這個panel就是為了設(shè)置容器用的,設(shè)置為StackPanel,WrapPanel都可以,足夠靈活,對應(yīng)上一條中的ItemsPresenter,
對應(yīng)blend菜單 :右鍵--àEdit Additional TemplatesàEdit? Layout? Items(ItemsPanel)?
?例如:
??????? <ItemsControl.ItemsPanel>
??????????? <ItemsPanelTemplate>
??????????????? <WrapPanel ?Name="wp" ?/>
??????????? </ItemsPanelTemplate>
??????? </ItemsControl.ItemsPanel>
?3.?最后是ItemTemplate , 每一個數(shù)據(jù)條目應(yīng)該是個什么樣子,負(fù)責(zé)具體呈現(xiàn)的,例如數(shù)據(jù)項是一個圖片,那就用圖片來顯示,
對應(yīng)blend菜單 :右鍵--à Edit Additional TemplatesàEdit ?Generated ?Items(ItemsTemplate)?
? <ItemsControl.ItemTemplate>
????? <DataTemplate>
???????? <Image Source="{Binding XPath=@}" />
???? </DataTemplate>
</ItemsControl.ItemTemplate>
4. 附帶一個,ItemContainerStyle ,也就是控制每個數(shù)據(jù)條目的顯示樣式,例如,想要在上例中的Image外面加一個邊框,
對應(yīng)blend菜單 :右鍵--à Edit Additional TemplatesàEdit? Generated? Item Container (ItemContainerStyle)?
?? <ItemsControl.ItemContainerStyle>
??????????? <Style TargetType="{x:Type ListBoxItem}">
??????????????? <Setter Property="Template">
??????????????????? <Setter.Value>
??????????????????????? <ControlTemplate TargetType="{x:Type ListBoxItem}">
??????????????????????????? <Border BorderBrush="Black" BorderThickness=".5" Height="100">
??????????????????????????????? <ContentPresenter />
??????????????????????????? </Border>
??????????????????????? </ControlTemplate>
??????????????????? </Setter.Value>
??????????????? </Setter>
??????????? </Style>
??????? </ItemsControl.ItemContainerStyle>
?
搞了很久才明白這么多東東,感覺微軟把這事搞復(fù)雜了,要是像asp.net 里面repeater或者datalist那種弄法,會簡單很多哦。
轉(zhuǎn)載于:https://www.cnblogs.com/xiaokang088/archive/2011/04/15/2016719.html
總結(jié)
以上是生活随笔為你收集整理的ItemsControl 解析的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 获取DataRow某列的值的封装
- 下一篇: 开一家奶茶店要多少钱 开奶茶店成本预算