【发布时间】:2018-07-16 18:21:57
【问题描述】:
我的 xaml 上有这段代码并且工作正常(ListView 组件)
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"></StackPanel>
</ItemsPanelTemplate>
我正在尝试使用此答案 Create DataTemplate in code behind 复制后面的代码,但我无法使其工作(Russell 的答案)。任何帮助将不胜感激。谢谢!
编辑:
ListView listView = new ListView();
listView.ItemsPanel = GetItemsPanelTemplate();
private ItemsPanelTemplate GetItemsPanelTemplate()
{
string xaml = @"<ItemsPanelTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation=""Horizontal""></StackPanel>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ItemsPanelTemplate>";
return XamlReader.Parse(xaml) as ItemsPanelTemplate;
}
【问题讨论】:
-
向我们展示您编写的不起作用的代码,我们会告诉您它有什么问题。
-
我编辑问题
-
您必须实际阅读错误消息...请将上面的 xaml 与您的 code-xaml-string 进行比较。后者将
ListView.ItemsPanel嵌套在ItemsPanelTemplate中,这当然是行不通的。 -
寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定问题或错误以及必要的最短代码在问题本身中重现它。
标签: wpf