Wpf ListBox未使用其ItemTemplate
这个列表框到底出了什么问题?它将项目显示为普通字符串,而不是使用我提供的模板:Wpf ListBox未使用其ItemTemplate,wpf,xaml,listbox,datatemplate,Wpf,Xaml,Listbox,Datatemplate,这个列表框到底出了什么问题?它将项目显示为普通字符串,而不是使用我提供的模板: <ListBox> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <Ellipse Width="20" Height="20" Fill="LightBlue" />
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Ellipse Width="20" Height="20" Fill="LightBlue" />
<TextBlock Text="{TemplateBinding Content}" Foreground="Red" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.Items>
<ListBoxItem>html</ListBoxItem>
<ListBoxItem>head</ListBoxItem>
<ListBoxItem>body</ListBoxItem>
<ListBoxItem>table</ListBoxItem>
<ListBoxItem>tr</ListBoxItem>
<ListBoxItem>td</ListBoxItem>
</ListBox.Items>
</ListBox>
html
头
身体
桌子
tr
运输署
你的XAML
<ListBox Margin="20" ItemsSource="{Binding Items}">
<ListBox.ItemTemplate>
<DataTemplate>
<Ellipse Width="20" Height="20" Fill="LightBlue" />
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
你的XAML
<ListBox Margin="20" ItemsSource="{Binding Items}">
<ListBox.ItemTemplate>
<DataTemplate>
<Ellipse Width="20" Height="20" Fill="LightBlue" />
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
中的备注部分: 在ItemsControl上设置ItemTemplate时,将生成UI 如下所示(以列表框为例): 1.在内容生成过程中,ItemsPanel向ItemContainerGenerator发出请求,为每个数据项创建一个容器。 对于ListBox,容器是ListBoxItem。发电机回叫 进入ItemsControl以准备容器 2.部分准备工作涉及将ListBox的ItemTemplate复制为ListBoxItem的ContentTemplate 3.与所有ContentControl类型类似,ListBoxItem的ControlTemplate包含ContentPresenter。应用模板时, 它创建一个ContentPresenter,其ContentTemplate绑定到 ListBoxItem的ContentTemplate 4.最后,ContentPresenter将该ContentTemplate应用于自身,并创建UI 当您直接在XAML中创建ListBoxItem实例时,显然不会执行这些步骤。但是,严格来说没有必要绑定
ItemSource
属性。您也可以直接设置如下项目:
<ListBox xmlns:sys="clr-namespace:System;assembly=mscorlib">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Ellipse Width="20" Height="20" Fill="LightBlue" />
<TextBlock Text="{TemplateBinding Content}" Foreground="Red" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.Items>
<sys:String>html</sys:String>
<sys:String>head</sys:String>
<sys:String>body</sys:String>
<sys:String>table</sys:String>
<sys:String>tr</sys:String>
<sys:String>td</sys:String>
</ListBox.Items>
</ListBox>
html
头
身体
桌子
tr
运输署
中的备注部分:
在ItemsControl上设置ItemTemplate时,将生成UI
如下所示(以列表框为例):
1.在内容生成过程中,ItemsPanel向ItemContainerGenerator发出请求,为每个数据项创建一个容器。
对于ListBox,容器是ListBoxItem。发电机回叫
进入ItemsControl以准备容器
2.部分准备工作涉及将ListBox的ItemTemplate复制为ListBoxItem的ContentTemplate
3.与所有ContentControl类型类似,ListBoxItem的ControlTemplate包含ContentPresenter。应用模板时,
它创建一个ContentPresenter,其ContentTemplate绑定到
ListBoxItem的ContentTemplate
4.最后,ContentPresenter将该ContentTemplate应用于自身,并创建UI
当您直接在XAML中创建ListBoxItem实例时,显然不会执行这些步骤。但是,严格来说没有必要绑定ItemSource
属性。您也可以直接设置如下项目:
<ListBox xmlns:sys="clr-namespace:System;assembly=mscorlib">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Ellipse Width="20" Height="20" Fill="LightBlue" />
<TextBlock Text="{TemplateBinding Content}" Foreground="Red" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.Items>
<sys:String>html</sys:String>
<sys:String>head</sys:String>
<sys:String>body</sys:String>
<sys:String>table</sys:String>
<sys:String>tr</sys:String>
<sys:String>td</sys:String>
</ListBox.Items>
</ListBox>
html
头
身体
桌子
tr
运输署
我只能假设这是因为您直接使用项目。如果您尝试绑定ItemsSource属性怎么办?@stukselbax:谢谢。出于某种奇怪的原因,这似乎奏效了。有什么见解可以解释为什么会这样吗?如果我有一个静态的项目列表,我想硬编码到XAML中怎么办?我只能假设这是因为您直接使用项目。如果您尝试绑定ItemsSource属性怎么办?@stukselbax:谢谢。出于某种奇怪的原因,这似乎奏效了。有什么见解可以解释为什么会这样吗?如果我有一个要硬编码到XAML中的静态项目列表呢?非常感谢。不过,我一直在寻找一个纯XAML解决方案,因为这是一个用户控件,没有与之相关联的ViewModel+1为备用方法。非常感谢。不过,我一直在寻找一个纯XAML解决方案,因为这是一个用户控件,没有与之相关联的ViewModel+1表示备用进近。