【问题标题】:How to paint specific rows in a listbox (WPF app)如何在列表框中绘制特定行(WPF 应用程序)
【发布时间】:2020-06-03 03:18:21
【问题描述】:

我正在编写一个项目,其中包含一个包含列表框的 WPF 窗口。列表框使用绑定到任务列表(我创建的一个类)。我正在尝试根据列表中项目的字段绘制列表框的一些特定行和一些特定行以具有彩色边框。如何将特定行的绘画/画笔方法添加到列表框而不是整个列表框? 下面是列表框的 XAML 属性:

<ListBox Name="Tasks" HorizontalAlignment="Left" ItemsSource="{Binding Path=Tasks,Mode=TwoWay}" Height="199" Margin="303,97,0,0" VerticalAlignment="Top" Width="439" RenderTransformOrigin="0.5,0.5">
            <ListBox.RenderTransform>
                <TransformGroup>
                    <ScaleTransform/>
                    <SkewTransform/>
                    <RotateTransform Angle="0.294"/>
                    <TranslateTransform/>
                </TransformGroup>
            </ListBox.RenderTransform>
        </ListBox>

【问题讨论】:

标签: c# wpf listbox


【解决方案1】:

ItemsControl 具有一个特殊功能,可以为控件中显示的项目选择模板。在下面的示例中,我说明了 DataTemplateSelector 的使用

首先是 XAML。在 Resource 部分中,为要在 ListBox 中显示的不同类型的项目定义了不同的数据模板,并且有对 DataTemplateSelector 的引用(稍后在 C# 代码中定义):

<Window x:Class="TestWpfApp.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:TestWpfApp"
        mc:Ignorable="d"
        x:Name="DataWindow"
        WindowState="Maximized"
        Title="MainWindow" Height="450" Width="800">
   <Window.Resources>
      <!-- The data template selector -->
      <local:DataItemTemplateSelector x:Key="DataItemTemplateSelector"/>
      <!-- Data template for a normal data item -->
      <DataTemplate x:Key="NormalDatatemplate">
         <Border Width="200"
                 Margin="0"
                 Background="White"
                 BorderThickness="2"
                 Padding="3"
                 BorderBrush="Transparent">
            <TextBlock VerticalAlignment="Center"
                       HorizontalAlignment="Stretch"
                       FontSize="20"
                       Foreground="Black"
                       Background="Transparent"
                       Text="{Binding Path=Header}"/>
         </Border>
      </DataTemplate>
      <!-- Data template for a special data item -->
      <DataTemplate x:Key="SpecialDatatemplate">
         <Border Width="200"
                 Margin="0"
                 Background="Yellow"
                 BorderThickness="2"
                 Padding="3"
                 BorderBrush="Blue">
            <TextBlock VerticalAlignment="Center"
                       HorizontalAlignment="Stretch"
                       FontSize="20"
                       Foreground="Black"
                       Background="Transparent"
                       Text="{Binding Path=Header}"/>
         </Border>
      </DataTemplate>
   </Window.Resources>
   <Grid>
      <ListBox x:Name="DataListBox"   
               Margin="50"
               Width="Auto"
               Height="Auto"
               HorizontalAlignment="Center"
               VerticalAlignment="Top"
               ItemsSource="{Binding ElementName=DataWindow, Path=DataList}"
               ItemTemplateSelector="{StaticResource ResourceKey=DataItemTemplateSelector}"/>
   </Grid>
</Window>

然后是 C# 代码,在那里定义了 DataItemTemplateSelector 和带有一些测试数据的 DataList:

using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace TestWpfApp
{
   // Different types of items on the list
   public enum DataItemType
   {
      Normal,
      Special
   }
   // Items on the list
   public class DataItem
   {
      public string Header { set; get; }
      public DataItemType DataType { set; get; }
   }
   // Data template selector for items on the list
   public class DataItemTemplateSelector : DataTemplateSelector
   {
      public override DataTemplate SelectTemplate(object dataItem, DependencyObject container)
      {
         // Check that everyting is OK
         if ((dataItem != null) && (dataItem is DataItem))
         {
            // Switch on the data type
            switch (((DataItem)(dataItem)).DataType)
            {
               case DataItemType.Normal:
                  // Return the data template for a normal data item 
                  return Application.Current.MainWindow.FindResource("NormalDatatemplate") as DataTemplate;
               case DataItemType.Special:
                  // Return the data template for a special data item 
                  return Application.Current.MainWindow.FindResource("SpecialDatatemplate") as DataTemplate;
               default:
                  return null;
            }
         }
         return null;
      }
   }
   /// <summary>
   /// Interaction logic for MainWindow.xaml
   /// </summary>
   public partial class MainWindow : Window
   {
      // The list of data to show in the list box
      public List<DataItem> DataList { set; get; }
      // The constructor of the main window
      public MainWindow()
      {
         // Some test data
         this.DataList = new List<DataItem>();
         this.DataList.Add(new DataItem() { Header = "Item 1", DataType = DataItemType.Normal });
         this.DataList.Add(new DataItem() { Header = "Item 2", DataType = DataItemType.Normal });
         this.DataList.Add(new DataItem() { Header = "Item 3", DataType = DataItemType.Special });
         this.DataList.Add(new DataItem() { Header = "Item 4", DataType = DataItemType.Normal });
         // Initializing the window
         InitializeComponent();
      }
   }
}

我希望您了解使用 DataTemplateSelector,然后您可以设置您的 ListBox 和其中不同类型的项目的样式

【讨论】:

    猜你喜欢
    • 2010-10-23
    • 1970-01-01
    • 2021-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-14
    • 1970-01-01
    相关资源
    最近更新 更多