簡體   English   中英

WPF ListView使用僅代碼背后的單元格更改前景

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

嗨,我有一個兩列的listview ,“ Folder ”和“ Status ”。 如何將“ 狀態 ”成員的前景更改為綠色(如果其數據為“ 鎖定 ”),將其更改為紅色(如果其數據為“ 未鎖定 ”)。

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

對不起,我的英語不好。

編輯: - - - - - - - - - - - - - - - - -

嘿,我嘗試了您的解決方案,但仍然無法正常工作。

看看我做錯了什么。

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

您可以將觸發器用於特定的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>

更新

我剛剛看到您使用命令式代碼尋求解決方案。 因此,您可以忽略我的回答,這是一種明確的解決方案。

但是請注意,您試圖實現的目標不應在后面的代碼中實現,因為它與MVVM原理不一致

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM