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>
相关推荐
weixin_447103587 小时前
Wpf布局之Canvas面板!
wpf
葬歌倾城8 小时前
waferMap图像渲染
c#·wpf
甄天9 小时前
WPF路由事件:冒泡、隧道与直接全解析
c#·wpf·visual studio
三千道应用题11 小时前
WPF学习笔记(12)下拉框控件ComboBox与数据模板
wpf
界面开发小八哥12 小时前
界面组件DevExpress WPF中文教程:Grid - 如何获取节点?
.net·wpf·界面控件·devexpress·ui开发
三千道应用题12 小时前
WPF学习笔记(22)项面板模板ltemsPanelTemplate与三种模板总结
wpf
佛·追命17 小时前
.net wpf混淆
.net·wpf
厦门德仔17 小时前
【WPF】外部引用样式
wpf·style
weixin_4471035819 小时前
Wpf布局之StackPanel!
wpf
小老鼠爱大米19 小时前
[C#] WPF - 资源URI
c#·wpf·uri