[英]How to add style for every listbox.Item in code behind?
我正在嘗試為項目中的每個列表框添加不同的 styles.Item。 誰能幫我解決這個問題?
`
foreach (var item in orderList)
{
var itm = new ListBoxItem();
if (item.CustomOrder)
{
itm.Content = item;
itm.Style = customOrderStyle;
listbox.Items.Add(itm);
}
else
{
itm.Content = item;
itm.Style = newOrderStyle;
listbox.Items.Add(itm);
}
}
`
我知道我正在將列表框項目添加到列表框,這就是不顯示內容的原因。 我也嘗試了一些不同的東西,但仍然不知道如何解決。
檢查數據模板概述鏈接和 go 到“將 DataTemplate 創建為資源”部分(並閱讀下一節)。 它展示了如何將DataTemplate
定義為資源以及如何使用DataTemplate.DataType
屬性。 只是不要給模板x:Key
,這樣 WPF 就可以根據項目類型自動選擇合適的模板。
然后為每個單獨的外觀項目定義一個數據 model 並停止您正在做的事情:不要顯式創建ListBoxItem
(讓 WPF 通過DataTemplate
來完成)。 而不是檢查CustomOrder
屬性,而是引入相關的 model 類,例如DefaultOrder
和CustomOrder
。 然后將這些類型的項目添加到您綁定到ListBox
的公共源集合中:
IOrder.cs
interface IOrder
{
...
}
默認訂單.cs
class DefaultOrder : IOrder
{
...
}
自定義訂單.cs
class CustomOrder : IOrder
{
...
}
主窗口.xaml
<Window>
<ListBox x:Name="OrdersOverview">
<ListBox.Resources>
<DataTemplate DataType="{x:Type local:DefaultOrder}">
...
</DataTemplate>
<DataTemplate DataType="{x:Type local:CustomOrder}">
...
</DataTemplate>
</ListBox.Resources>
</ListBox>
</Window>
MainWindow.xaml.cs
prtial class MainWindow : Window
{
private ObservableCollection<IOrder> Orders { get; }
public MainWindow()
{
InitializeComponent();
this.Orders = new ObservableCollection<IOrder>();
this.OrdersOverview.ItemsSource = this.Orders;
}
private void CreateDefaultOrder()
{
var newOrder = new DefaultOrder();
// Show the new order in the ListBox
this.Orders.Add(newOrder);
}
private void CreateCustomOrder()
{
var newOrder = new CustomOrder();
// Show the new order in the ListBox
this.Orders.Add(newOrder);
}
}
一個更典型的 WPF 實現的例子:
namespace Core2022.SO.Ali.ListBoxItemStyleSelector
{
public class OrderItem
{
public bool CustomOrder { get; set; }
public int Id { get; set; }
}
}
using System.Collections.ObjectModel;
namespace Core2022.SO.Ali.ListBoxItemStyleSelector
{
public class OrderViewModel
{
public ObservableCollection<OrderItem> Orders { get; } = new ObservableCollection<OrderItem>();
public OrderViewModel()
{
for (int i = 0; i < 10; i++)
{
Orders.Add(new OrderItem() { Id = i, CustomOrder = i % 2 == 0 });
}
}
}
}
using System;
using System.Windows;
using System.Windows.Controls;
namespace Core2022.SO.Ali.ListBoxItemStyleSelector
{
public class CustomOrderStyleSelector : StyleSelector
{
public Style? CustomOrderTrue { get; set; }
public Style? CustomOrderFalse { get; set; }
public override Style SelectStyle(object item, DependencyObject container)
{
if (item is OrderItem order)
{
return (order.CustomOrder ? CustomOrderTrue : CustomOrderFalse)
?? throw new NullReferenceException(); ;
}
return base.SelectStyle(item, container);
}
}
}
<Window x:Class="Core2022.SO.Ali.ListBoxItemStyleSelector.OrdersWindow"
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:Core2022.SO.Ali.ListBoxItemStyleSelector"
mc:Ignorable="d"
Title="OrdersWindow" Height="300" Width="200"
DataContext="{DynamicResource vm}">
<FrameworkElement.Resources>
<local:OrderViewModel x:Key="vm"/>
<local:CustomOrderStyleSelector x:Key="customOrderStyleSelector">
<local:CustomOrderStyleSelector.CustomOrderTrue>
<Style TargetType="ListBoxItem">
<Setter Property="Foreground" Value="Green"/>
<Setter Property="FontSize" Value="15"/>
</Style>
</local:CustomOrderStyleSelector.CustomOrderTrue>
<local:CustomOrderStyleSelector.CustomOrderFalse>
<Style TargetType="ListBoxItem">
<Setter Property="Background" Value="Coral"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
</Style>
</local:CustomOrderStyleSelector.CustomOrderFalse>
</local:CustomOrderStyleSelector>
</FrameworkElement.Resources>
<Grid>
<ListBox ItemsSource="{Binding Orders}"
DisplayMemberPath="Id"
ItemContainerStyleSelector="{DynamicResource customOrderStyleSelector}"/>
</Grid>
</Window>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.