我正在尝试为(PropertyDescriptors 的)PropertyCollection 创建 DataTemplates,以这种格式显示集合中的项目,
描述符 1 > 描述符 2 > 描述符 3 > 描述符 4 > 描述符 5
每个描述符都是一个链接(使用超链接),我的问题是我可以使用标签(不是 ItemsControl 主机)来做到这一点吗?如果是,谁能举个例子说明如何使用 DataTemplates 实现这一点?
此外,是否可以从 DataTemplate 访问属性描述符?例如,假设我想使用属性描述符的当前实例作为 CommandParameter。
感谢任何帮助,谢谢。
最佳答案
您可以尝试使用带有 ItemsPanel 的 ListBox 来执行此操作,该 ItemsPanel 对项目使用水平布局(在我的示例中,我只是绑定(bind)到一个字符串列表)。 HTH.
代码:
public List<string> Properties { get; set; }
XAML:
<ListBox ItemsSource="{Binding Properties}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Width="{Binding (FrameworkElement.ActualWidth), RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}"
ItemWidth="{Binding (ListView.View).ItemWidth, RelativeSource={RelativeSource AncestorType=ListView}}"
MinWidth="{Binding ItemWidth, RelativeSource={RelativeSource Self}}"
ItemHeight="{Binding (ListView.View).ItemHeight, RelativeSource={RelativeSource AncestorType=ListView}}" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock>
<Hyperlink NavigateUri="{Binding}">
<TextBlock Text="{Binding StringFormat={}{0} >}"/>
</Hyperlink>
</TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
截图:
关于c# - 如何创建带链接的 DataTemplate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3302429/