2011-03-06 52 views
1

我有一個外部'當前'屬性(int),它表示集合的當前索引。我有一個顯示這個集合的列表視圖。我希望能夠根據「當前」值設置集合的「第n個」項目,即當前值爲3時,突出顯示集合中的第4個項目(索引= 3)等。我該怎麼做?樣式在listview中的第n項?

另一種方法是在項目文本等於另一個外部屬性時進行綁定。

回答

0

要自定義ListView的樣式,可以創建一個DataTrigger,該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
  4. DataContext的一個DataTrigger是一個列表客戶對象與用戶定義的StyleType屬性在代碼隱藏中初始化
  5. Button Command清除StyleType的值
  6. 點擊一行改變下一行
  7. 客戶實現INotifyPropertyChanged的風格,即火災時,StyleType改變
  8. DataTrigger改變背景每個ListViewItem的

這裏是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> 

這裏是視圖模型視圖用於獲取客戶和改變StyleType的價值:

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"); 
    } 
    } 
} 
} 

這裏是模型,ViewModelBase實現INotifyPropertyChanged,當改變了更新每個ListViewItem的StyleType火災OnPropertyChanged:

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(); 
    } 
    } 
} 
+0

謝謝,但我在這段代碼中看不到我的問題的答案。當Count == 2時第三項被突出顯示,Count == 3時第四項被突出顯示等等?我也不想修改我的模型專門爲造型目的。 – DaveO 2011-03-07 04:11:08

+0

提出我的答案的目標是證明您可以使用網格中綁定的數據來控制顯示。例如,如果用戶可以對視圖中的數據進行排序,那麼應用該信息的方式可能會很困難。我已經調整了我的答案,根據當前選定的行更改下一行的樣式,但是我會補充說我沒有解決處理排序問題。如果您找到了更好的方法,請發佈您的解決方案,我會爲此投票。 – Zamboni 2011-03-08 04:07:59