作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有以下 XAML:
<Window x:Class="ImageComparing.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="350" Width="525" xmlns:my="clr-namespace:ImageComparing" Title="Image comparing">
<DockPanel>
<ToolBar Name="toolbar1" DockPanel.Dock="Top" Height="41" Background="#FFA5D95A">
/*other content*/
</ToolBar>
<WrapPanel Name="wrapPanel1" >
/*other content*/
<Label Content="Label" Height="28" Name="label1" />
</WrapPanel>
</DockPanel>
</Window>
我想向 wrapPanel1
添加内容 - 我尝试了以下代码:
if (info.Attributes == FileAttributes.Directory)
wrapPanel1.Children.Add(new FolderEntry(info.Name));
else
wrapPanel1.Children.Add(new FileEntry(info.Name));
由于某种原因,项目没有显示。我该如何解决这个问题?
最佳答案
您应该使用一些 ItemsControl
,然后从项目源属性中添加/删除项目。您可以使用 ItemsPanel
属性或更改模板。例如,使用 ListBox
并设置 Template
属性:
<ListBox Grid.Row="2" ItemsSource="{Binding Items}">
<ListBox.Template>
<ControlTemplate>
<WrapPanel IsItemsHost="True"/>
</ControlTemplate>
</ListBox.Template>
</ListBox>
现在,当您在 ViewModel/DataContext 的 Items
属性中添加或删除项目时,项目将使用 WrapPanel
显示,而不是 StackPanel
是 ListBox
的默认值。
希望这有助于...
关于c# - 如何将项目动态添加到 WrapPanel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24248898/
我是一名优秀的程序员,十分优秀!