wpf UniformGrid 动态加载数据

在WPF中,如果你想要在UniformGrid内部为每个Model对象放置一个Panel(比如StackPanelGrid),并且这些Panel是通过数据绑定动态生成的,你需要结合使用ItemsControlDataTemplate以及UniformGrid。但是,由于UniformGrid不是ItemsControl的直接子类,你需要将UniformGrid作为ItemsPanelTemplate的内容嵌入到ItemsControl中。

cs 复制代码
public class MyModel  
{  
    public string Name { get; set; }  
    // 可能还有其他属性  
}
  1. XAML中的绑定 :在你的XAML中,使用ItemsControl并设置其ItemsSource为你的Model集合。然后,通过ItemsPanelTemplate指定使用UniformGrid作为布局,并在ItemTemplate中定义每个Model对象如何显示(即每个Model对象被放置在一个Panel中)。

    cs 复制代码
    <Window x:Class="YourNamespace.MainWindow"  
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
            Title="MainWindow" Height="350" Width="525">  
        <Grid>  
            <ItemsControl ItemsSource="{Binding Models}">  
                <ItemsControl.ItemsPanel>  
                    <ItemsPanelTemplate>  
                        <UniformGrid Rows="2" Columns="2" /> <!-- 根据需要设置行数和列数 -->  
                    </ItemsPanelTemplate>  
                </ItemsControl.ItemsPanel>  
                <ItemsControl.ItemTemplate>  
                    <DataTemplate>  
                        <StackPanel Orientation="Horizontal" Margin="5"> <!-- 使用StackPanel作为每个Model的容器 -->  
                            <TextBlock Text="{Binding Name}" />  
                            <!-- 根据需要添加其他控件 -->  
                        </StackPanel>  
                    </DataTemplate>  
                </ItemsControl.ItemTemplate>  
            </ItemsControl>  
        </Grid>  
    </Window>
相关推荐
Zhen (Evan) Wang3 小时前
.NET 6 + Dapper + User-Defined Table Type
sqlserver·c#·.net·wpf
界面开发小八哥1 天前
界面控件DevExpress WPF v25.1预览 - 支持Windows 11系统强调色
windows·wpf·界面控件·devexpress·ui开发·.net 9
军训猫猫头1 天前
89.WPF 中实现便捷的数字输入框:DecimalUpDown 控件的使用 WPF例子 C#例子.
开发语言·c#·wpf
Pasregret1 天前
缓存与数据库一致性深度解析与解决方案
数据库·缓存·wpf
Java林间3 天前
Zookeeper是什么?基于zookeeper实现分布式锁
分布式·zookeeper·wpf
zizisuo3 天前
1.微服务拆分与通信模式
微服务·wpf
程序员秘密基地3 天前
基于c#,wpf,ef框架,sql server数据库,音乐播放器
sql·sqlserver·c#·.net·wpf
Zhen (Evan) Wang3 天前
.NET 6 WPF 利用CefSharp.Wpf.NETCore显示PDF文件
.net·wpf·.netcore
冰茶_4 天前
WPF特性分析
学习·microsoft·c#·wpf
qq_196055875 天前
最快打包WPF 应用程序
wpf