繁体   English   中英

动态将项目添加到网格WPF

[英]Dynamically add items to grid WPF

我有以下“网格”:

<Grid x:Name="ImagesGrid" Grid.Row="1"  >
    <ItemsControl ItemsSource="{Binding FrameViewers}" />
</Grid>

我还具有以下UserControl项目集合:

private ObservableCollection<FrameViewer> m_frameViewers = new ObservableCollection<FrameViewer>();

public ObservableCollection<FrameViewer> FrameViewers
{
    get => m_frameViewers;
    set
    {
        m_frameViewers = value;
        OnPropertyChanged();
    }
}

我想动态地将FrameViewer添加到我的屏幕,使其看起来像所附的图像: 在此处输入图片说明

目前,我看到它们像这样垂直排列: 在此处输入图片说明

我能够使用“网格”,并在ItemSource添加了一个StackPanel ,但是它们都按照标题的长度进行了大小调整,并附加在Grid的左侧

我在这里想念什么? 我想念什么?

默认情况下,ItemsControl将垂直排列项目。

对于水平布局,你需要改变它的ItemsPanel ,到不同的ItemsPanelTemplate -使用一个水平的StackPanel,或像一个UniformGrid,这取决于你想要的时候有集合中三个以上FrameViewer项目发生什么。

请参阅此处的一些示例。

然后,您需要将ItemsControl ItemTemplate设置为适当的DataTemplate ,以确保以所需的大小显示每个FrameViewer项。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM