17

My ListView looks like: http://oi36.tinypic.com/ek5n3o.jpg

My listview xaml:

<ListView  Name="notListView" Width="550" HorizontalAlignment="Left">
        <ListView.View>
        <GridView AllowsColumnReorder="true">
            <GridViewColumn Header="Ders Kodu" Width="100" DisplayMemberBinding="{Binding Path=DersKodu}" />
            <GridViewColumn Header="Ders Adı" Width="200" DisplayMemberBinding="{Binding Path=DersAdi}" />
            <GridViewColumn Header="Vize" Width="50" DisplayMemberBinding="{Binding Path=Vize}" />
            <GridViewColumn Header="Final" Width="50" DisplayMemberBinding="{Binding Path=Final}" />
            <GridViewColumn Header="Ortalama" Width="60" DisplayMemberBinding="{Binding Path=Ortalama}" />
            <GridViewColumn Header="Harf Notu" Width="60" DisplayMemberBinding="{Binding Path=Harf}" />
        </GridView>
    </ListView.View>
</ListView>

My .cs code:

notListView.ItemsSource = notGoruntule(1, 1); // notGoruntule() function returns an Arraylist, which contains my "Notlar" objects.

I tried this:

ListViewItem lvitem = (ListViewItem)notListView.Items[0];
lvitem.Background = Brushes.Red;

But first line throws:

Unable to cast object of type 'OBS_Interface_5.Classes.Notlar' to type 'System.Windows.Controls.ListViewItem'.

How i solve this problem?

4

1 回答 1

74

您可以直接StyleListViewItemxaml 中,

例子:

假设您的“Harf”变量是一个字符串,您可以尝试

<ListView Name="notListView"
          Width="550"
          HorizontalAlignment="Left">
  <ListView.Resources>
    <Style TargetType="{x:Type ListViewItem}">
      <Style.Triggers>
        <DataTrigger Binding="{Binding Harf}"
                      Value="1">
          <Setter Property="Background"
                  Value="Red" />
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </ListView.Resources>
  ...

现在任何ListView“Harf”值为 1 的行都将具有“红色”Background

于 2013-04-21T00:06:29.000 回答