WPF中ItemsControl的模板设置

这篇博客介绍了如何利用ItemsControl和DataTemplate在WPF中创建一个自定义布局的按钮,通过设置ItemsControl的ItemsPanel和ItemTemplate,实现了按钮内容的左右排列,并通过设置Button的样式调整了边框和背景。同时,文中强调了ItemsControl没有默认的可视实现,需要通过模板来定义其外观。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

					<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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值