WPF ListView ItemTemplate 无法在 属性 个元素上设置属性

WPF ListView ItemTemplate Cannot set properties on property elements

我正在 WPF 中处理 ListView,我希望 ListViewViewModel 中获取 ItemLists,但出现以下错误 无法在 属性 个元素上设置属性

xaml代码:

<ListView ItemsSource="{Binding MenuItems}" Name="mainSideMenuList" ScrollViewer.HorizontalScrollBarVisibility="Disabled" Background="#FF284593" Foreground="#FF3457D1">
   <i:Interaction.Triggers>
      <i:EventTrigger EventName="MouseDoubleClick">
         <i:InvokeCommandAction
            Command="{Binding Command}"
            CommandParameter="{Binding ElementName=mainSideMenuList, Path=SelectedItem}" />
      </i:EventTrigger>
   </i:Interaction.Triggers>
   <ListView.ItemTemplate Height="60">
      <DataTemplate>
         <StackPanel Orientation="Horizontal">
            <materialDesign:PackIcon Kind="{Binding IconKind}" Width="25" Height="25" Margin="10" VerticalAlignment="Center"/>
            <TextBlock Text="{Binding Name}" VerticalAlignment="Center" Margin="10 10" />
         </StackPanel>
      </DataTemplate>
   </ListView.ItemTemplate>
</ListView>

视图模型:

private readonly ItemHandler _itemHandler;
private ICommand _command;
public MainWindowViewModel() {
 _itemHandler = new ItemHandler();
 _itemHandler.Add(new Item("Settings", PackIconKind.Settings));
 _itemHandler.Add(new Item("Products", PackIconKind.FoodForkDrink));
 _itemHandler.Add(new Item("Tickets", PackIconKind.Ticket));
 _itemHandler.Add(new Item("Entities", PackIconKind.Table));
 _itemHandler.Add(new Item("Accounts", PackIconKind.Calculator));
 _itemHandler.Add(new Item("Inventory", PackIconKind.Database));
 _itemHandler.Add(new Item("Printing", PackIconKind.Printer));
 _itemHandler.Add(new Item("Reports", PackIconKind.FileAccount));
 _itemHandler.Add(new Item("Automation", PackIconKind.Calculator));
 _itemHandler.Add(new Item("Users", PackIconKind.User));
}
public List < Item > MenuItems {
 get {
  return _itemHandler.MenuItems;
 }
}
public ICommand Command {
 get {
  return _command ? ? (_command = new RelayCommand(x => {
   DoStuff(x as Item);
  }));
 }
}

private void DoStuff(Item item) {
 MessageBox.Show(item.Name + " element clicked");
}

我做错了什么,我该如何解决?

问题是 ListView.ItemTemplate 是类型 DataTemplate 而不是 FrameworkElement 所以它不能有任何 HeightWidth 或者与 UI-Elements 有关的任何其他内容。它只能有一个Content,这是一个DataTemplate,它可以容纳一个FrameworkElement,主要是一个Panel。然后这些元素可以有一个 heightwidth 或任何 UI-Element 属性.

因此删除<ListView.ItemTemplate Height="60">Height并设置为<StackPanel Orientation="Horizontal">