2013-03-26 59 views
1

我有一個「播放列表」,我想強調的「正在播放」項。WPF列表框DataTrigger不火

我想這

<!-- Row 5: Playlist --> 
    <ListBox x:Name="Tracks" MinHeight="400" Grid.Row="5" Margin="20"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <DockPanel Height="44" Width="436" Dock="Left"> 
        <StackPanel Orientation="Vertical" Width="374" Name="Wrapper"> 
         <Label Content="{Binding Path=title}" Name="Test" Foreground="CornflowerBlue" FontSize="14" Padding="0" /> 
         <Label Content="{Binding Path=artist}" Foreground="DarkGray"  FontSize="14" Padding="0" /> 
        </StackPanel> 
        <Label Content="{Binding Path=DurationFormatted}" Foreground="DarkGray" Width="62" Padding="0" DockPanel.Dock="Right" HorizontalContentAlignment="Right" /> 
       </DockPanel> 
       <DataTemplate.Triggers> 
        <DataTrigger Binding="{Binding Path=NowPlaying}" Value="True"> 
         <Setter TargetName="Wrapper" Property="Background" Value="LightBlue"/> 
         <Setter TargetName="Test" Property="FontSize" Value="24"/> 
         <Setter Property="ListBoxItem.Foreground" Value="Red" /> 
        </DataTrigger> 
       </DataTemplate.Triggers> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 

<ListBox.ItemTemplate> 
same stuff without Triggers section 
<ListBox.ItemContainerStyle> 
    <Style TargetType="{x:Type ListBoxItem}"> 
     <Style.Triggers> 
      <DataTrigger Binding="{Binding Path=NowPlaying}" Value="True"> 
       <Setter Property="ListBoxItem.Background" Value="Red" /> 
      </DataTrigger> 
     </Style.Triggers> 
    </Style> 
</ListBox.ItemContainerStyle> 

NowPlaying只是音頻模型中的布爾屬性,我在調試器,當前的對象確實得到NowPlaying ==真檢查。但是這兩個觸發器都不會改變項目外觀。我做錯了什麼? 此外,對於我來說,我更喜歡命令式的風格。從代碼隱藏這樣做很容易嗎?

P.S.我爲測試設置了強調項目的極端值:)

回答

4

記住您需要通過實施INotifyPropertyChanged接口來提高NowPlaying屬性?如果沒有,綁定引擎無法看到屬性被改變,因此沒有更新可視圖進行。

而只是一個小加法:我肯定會贊成第一種方法,儘量避免datatrigger,其實在風格,儘可能有關數據綁定的所有業務數據。樣式應該只包含視圖設計中,的DataTemplates用於顯示真實的數據。如果你保持這種分離,它更容易重用一種風格。

+0

謝謝,我不知道INotifyPropertyChanged的。 – Tommi 2013-03-26 12:29:02