<ItemsControl>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>如果我想将ItemsPanelTemplate从默认(StackPanel)更改为网格,则在XAML中执行上述操作。我如何在代码中实现同样的目标?
我读了这个这里,但搞不懂。
发布于 2017-06-14 23:41:42
我更愿意使用Generic.xaml内部自定义控件的默认样式来实现这一点,但是如果您想要一种纯C#方式,下面是如何实现的-
private void ApplyGridAsItemsPanel()
{
MyItemsControl.ItemsPanel = ParseItemsPanelTemplate(typeof(Grid));
ItemsPanelTemplate ParseItemsPanelTemplate(Type panelType)
{
var itemsPanelTemplateXaml =
$@"<ItemsPanelTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
<{panelType.Name} />
</ItemsPanelTemplate>";
return (ItemsPanelTemplate)XamlReader.Load(itemsPanelTemplateXaml);
}
}https://stackoverflow.com/questions/44555462
复制相似问题