Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
969 views
in Technique[技术] by (71.8m points)

listviewitem - How i change ListView Item Background Color according to listview item HarfNotu value in wpf

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can Style the ListViewItem in xaml directly,

Example:

Assuming your "Harf" variable is a string, you can try

<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>
  ...

Now any ListView Row with "Harf" Value of 1 will have a "Red" Background


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...