在listview中设计第n项?

时间:2011-03-06 02:33:47

标签: wpf listview styles

我有一个外部'current'属性(int),它表示集合的当前索引。我有一个列表视图,显示此集合。我希望能够根据'current'的值设置集合的'nth'项,即如果current是3,则突出显示集合中的第4项(index = 3)等。我该怎么做?

另一种方法是在项目文本等于另一个外部属性时进行绑定。

1 个答案:

答案 0 :(得分:0)

要自定义ListView的样式,您可以创建一个DataTrigger,它绑定到View的DataContext的属性以更改当前样式。在此示例中,代码更改背景。

<ListView.ItemContainerStyle>
  <Style TargetType="ListViewItem">
    <Setter Property="Background" Value="Aqua"/>
      <Style.Triggers>
        <DataTrigger Binding="{Binding Path=DataContext.StyleType, RelativeSource={RelativeSource Self}}" Value="1">
          <Setter Property="Background" Value="Chartreuse"/>
        </DataTrigger>
      </Style.Triggers>
  </Style>
</ListView.ItemContainerStyle>

我已经在这里添加了大部分代码,因此您可以尽可能完整地了解正在发生的事情,但我跳过了常见的MVVM基类。

工作原理:

  1. ListView ItemsSource绑定到客户
  2. ListView SelectedItem绑定到客户
  3. ListView.ItemContainerStyle有一个绑定到DataContext.StyleType的DataTrigger
  4. DataContext是一个Customer对象列表,其中包含用户定义的StyleType属性,该属性在代码隐藏中初始化
  5. 按钮命令清除StyleType
  6. 的值
  7. 单击一行会更改下一行的样式
  8. Customer实现了在StyleType更改时触发的INotifyPropertyChanged
  9. DataTrigger改变背景每个ListViewItem
  10. 这是XAML,请看DataTrigger:

    <Window x:Class="ListViewScrollPosition.Views.ScrollBarwindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="Style on Model" Height="300" Width="300">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <ListView 
                Grid.Row="0"
                SelectedItem="{Binding Customer}"
                ItemsSource="{Binding Customers}" x:Name="myListView">
                <ListView.ItemContainerStyle>
                    <Style TargetType="ListViewItem">
                        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                        <Setter Property="Background" Value="Aqua"/>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Path=DataContext.StyleType, RelativeSource={RelativeSource Self}}" Value="1">
                                <Setter Property="Background" Value="Pink"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </ListView.ItemContainerStyle>
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="First Name"
                                        DisplayMemberBinding="{Binding FirstName}" />
                        <GridViewColumn Header="Last Name"
                                        DisplayMemberBinding="{Binding LastName}" />
                        <GridViewColumn Header="Style Type"
                                        DisplayMemberBinding="{Binding StyleType}" />
                    </GridView>
                </ListView.View>
            </ListView>
            <Button Grid.Row="1" Content="Change Style" Command="{Binding Path=AlterStyle}"/>
        </Grid>
    </Window>
    

    以下是View用于获取Customers并更改StyleType值的ViewModel:

    using System.Collections.Generic;
    using System.Windows.Input;
    using ListViewScrollPosition.Commands;
    using ListViewScrollPosition.Models;
    
    namespace ListViewScrollPosition.ViewModels
    {
     public class MainViewModel : ViewModelBase
     {
       private DelegateCommand _alterStyleCommand;
    
       public MainViewModel()
       {
       }
    
       public ICommand AlterStyle
       {
         get
         {
           if (_alterStyleCommand == null)
            {
              _alterStyleCommand = new DelegateCommand(AlterStyleCommand);
            }
           return _alterStyleCommand;
         }
       }
    
       private void AlterStyleCommand()
       {
        foreach (var customer in Customers)
        {
          customer.StyleType = 0;
        }
       }
    
       private void ApplyStyleToNextRow(Customer currentCustomer)
       {
         bool setNext = false;
         foreach (var customer in Customers)
         {
           if (setNext)
           {
             customer.StyleType = 1;
             setNext = false;
           }
           else
           {
             customer.StyleType = 0;
           }
    
           if (currentCustomer == customer)
           {
             setNext = true;
           }
         }
       }
    
       private List<Customer> _customers = Customer.GetSampleCustomerList();
       public List<Customer> Customers
       {
         get
         {
            return _customers;
         }
       }
    
       private Customer _customer = null;
       public Customer Customer
       {
         get
         {
           return _customer;
         }
         set
         {
           _customer = value;
           ApplyStyleToNextRow(_customer);
           OnPropertyChanged("Customer");
         }
       }
     }
    }
    

    这是Model,ViewModelBase实现了INotifyPropertyChanged,StyleType在更改时触发OnPropertyChanged,更新每个ListViewItem:

    using System;
    using System.Collections.Generic;
    
    namespace ListViewScrollPosition.Models
    {
      public class Customer : ViewModels.ViewModelBase
      {
        public String FirstName { get; set; }
        public String LastName { get; set; }
    
        private int _style;
        public int StyleType
        {
          get { return _style;}
          set
          {
            _style = value;
            OnPropertyChanged("StyleType");
          }
        }
    
        public Customer(String firstName, String lastName, int styleType)
        {
          this.FirstName = firstName;
          this.LastName = lastName;
          this.StyleType = styleType;
        }
    
        public static List<Customer> GetSampleCustomerList()
        {
         return new List<Customer>(new Customer[4] {
                new Customer("A.", "Zero", 0), 
                new Customer("B.", "One", 1),
                new Customer("C.", "Two", 2),
                new Customer("D.", "Three", 1)
            });
        }
      }
    }
    

    以下是设置DataContext的代码隐藏:

    using System.Windows;
    namespace ListViewScrollPosition.Views
    {
      public partial class ScrollBarwindow : Window
      {
        public ScrollBarwindow()
        { 
          InitializeComponent();
          DataContext = new ViewModels.MainViewModel();
        }
      }
    }