简体   繁体   中英

WPF ListView alter foreground using codebehind for just a cell

Hi I have a listview with two columns, " Folder " & " Status ". How do I change the foreground of the " Status " members to green if its data is " Locked " and to red if its data is " Unlocked ".

Example

Folder    |       Status
----------+--------------------------------------------
xxxx      |       Locked  <--To be appeared as green
yyyyy     |       Unlocked <-- To be appeared as red

Sorry for my bad english.

EDIT:---------------------------------

Hey i tried your solution but i still cant get it to work.

Take a look at what i done wrong.

<ListView x:Name="FoldersListView" Margin="11,202,8,98" Foreground="Black" Background="#FFFFCFCF" BorderBrush="Transparent" FontWeight="Bold">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Folder" Width="300" DisplayMemberBinding="{Binding Path = FolderPath}"/>

                <GridViewColumn Header="Status" Width="100" DisplayMemberBinding="{Binding Path = FolderStatus}">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate> 
                            <TextBlock Text="{Binding Path=Locked}"> 
                                <TextBlock.Style> 
                                    <Style TargetType="TextBlock"> 
                                        <Setter Property="Foreground" Value="Red"/> 
                                        <Style.Triggers> 
                                            <DataTrigger Binding="{Binding Path=Locked}" Value="True"> 
                                                  <Setter Property="Foreground" Value="Green"/> 
                                            </DataTrigger> 
                                            <DataTrigger Binding="{Binding Path=Locked}" Value="False"> 
                                                  <Setter Property="Foreground" Value="Red"/> 
                                            </DataTrigger> 
                                        </Style.Triggers> 
                                    </Style> 
                           </TextBlock.Style> 
                          </TextBlock> 
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>

You can use triggers for a particular CellTemplate

<DataTemplate> 
 <TextBlock Text="{Binding Path=Status}"> 
 <TextBlock.Style> 
   <Style TargetType="TextBlock"> 
     <Setter Property="Foreground" Value="Red"/> 
     <Style.Triggers> 
       <DataTrigger Binding="{Binding Path=Locked}" Value="True"> 
          <Setter Property="Foreground" Value="Green"/> 
        </DataTrigger> 
       <DataTrigger Binding="{Binding Path=Locked}" Value="False"> 
          <Setter Property="Foreground" Value="Red"/> 
        </DataTrigger> 
      </Style.Triggers> 
     </Style> 
   </TextBlock.Style> 
  </TextBlock> 
<DataTemplate>

UPDATE

I just saw you asked for the solution using an imperative code. Therefore, you can ignore my answer which is a solution for a declaritive way.

Please however note that the goal you are trying to achieve should not be implemented in code behind as it's not consistent with the MVVM principles

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM