0

こんにちは私はlistview2つの列、「フォルダ」と「ステータス」を持っています。「ステータス」メンバーの前景を、データが「ロック」の場合は緑に、データが「ロック解除」の場合は赤に変更するにはどうすれよいですか。

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

1 に答える 1

1

特定の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>

アップデート

命令型コードを使用して解決策を求められたのを見たばかりです。したがって、宣言的な方法の解決策である私の答えを無視することができます。

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

于 2012-08-02T12:23:41.440 に答える