<ItemsControl x:Name="_userItemsControl" ItemsSource="{Binding UserInfos}" Width="1824" ScrollViewer.HorizontalScrollBarVisibility="Hidden">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button BorderThickness="0 1 0 1" BorderBrush="#D0CFD4" Background="White">
<Grid Width="{Binding RelativeSource={RelativeSource AncestorType=ItemsControl},Path=Width}">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Margin="30" Text="{Binding UserName}" FontSize="50" Foreground="Black"/>
<TextBlock Grid.Column="1" Margin="30" Text=">" FontSize="50" Foreground="#959596" HorizontalAlignment="Right"/>
</Grid>
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
最后效果如下
这样我们就将一个按钮作为一个itemsControl控件的子项。下面转发网页的总结:
ItemsControl 没有缺省的可视实现. 使用Template 属性定义ItemsControl的外观.
ItemsPresenter 使用指定的ItemsPanelTemplate布局其中包含的项
使用ItemTemplate 设置 DataTemplate 定义数据的外观.
使用ItemContainerStyle 指定包含数据的container的外观.
————————————————
原文链接:https://blog.csdn.net/theoldsod2000/article/details/12168353