WPF ListBox利用DataTemplate数据绑定后,只显示一行,没有显示多行

叶晨- 2020-07-27 11:47:36
XAML代码:
<Window x:Class="WPF_125P.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPF_125P"
mc:Ignorable="d"
Title="Date Converter" Height="266" Width="300">
<Window.Resources>
<!--以资源的形式创建两个实例-->
<local:CategoryToSourceConverter x:Key="cts"/>
<local:StateToNullableBoolConverter x:Key="stnb"/>
</Window.Resources>

<Grid>
<StackPanel Background="LightBlue">
<ListBox x:Name="listBoxPlane" Height="160" Margin="5" Background="White">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<!--<Image Width="20" Height="20"
Source="{Binding Path=category,Converter={StaticResource cts}}"/>-->
<TextBlock Text="{Binding Path=Name}" Width="60" Height="80,0"/>
<TextBlock Text="{Binding Path=Name}" Width="60" Height="80,0"/>
<!--<CheckBox IsThreeState="True"
IsChecked="{Binding Path=state,Converter={StaticResource stnb}}"/>-->
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button x:Name="buttonLoad" Content="Load" Height="25" Margin="5,0"
Click="buttonLoad_Click"/>
<Button x:Name="buttonSave" Content="Save" Height="25" Margin="5"
Click="buttonSave_Click"/>
</StackPanel>
<!--<Image Source="Icons\Bomber.png" Width="200" Height="200"/>-->
</Grid>
</Window>

C#关键代码:
private void buttonLoad_Click(object sender, RoutedEventArgs e)
{

List<Plane> planes = new List<Plane>()
{
new Plane(){category=Category.Fighter,Name="B-1",state=State.Unknown},
new Plane(){category=Category.Bomber,Name="B-2",state=State.Unknown},
new Plane(){category=Category.Fighter,Name="F-22",state=State.Unknown},
new Plane(){category=Category.Fighter,Name="Su-47",state=State.Unknown},
new Plane(){category=Category.Bomber,Name="B-52",state=State.Unknown},
new Plane(){category=Category.Fighter,Name="J-10",state=State.Unknown},
};
this.listBoxPlane.ItemsSource = planes;
}

...全文
4732 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
E次奥 2020-07-29
  • 打赏
  • 举报
回复

<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
八爻老骥 2020-07-28
  • 打赏
  • 举报
回复
改成这样:
<DataTemplate>
<StackPanel Orientation="Vertical">
欧皇神降术 2020-07-28
  • 打赏
  • 举报
回复 1
显示了一行 没显示多行 你确定不考虑一下是不是你的UI问题?你这listbox的高度160 一个子项的高度就是160(俩个textblock 80+80)

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧