简体   繁体   English

WPF ListView使用仅代码背后的单元格更改前景

[英]WPF ListView alter foreground using codebehind for just a cell

Hi I have a listview with two columns, " Folder " & " Status ". 嗨,我有一个两列的listview ,“ 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 您可以将触发器用于特定的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 但是请注意,您试图实现的目标不应在后面的代码中实现,因为它与MVVM原理不一致

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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