Reputation: 3247
I have a "playlist" and I want to emphasize "now playing" item.
I tried this
<!-- 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>
And this
<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 is just a bool property of audio model and I checked in debugger that current object indeed get NowPlaying == true. But both of this triggers does not change item look. What I'm doing wrong? Also, as for me, I prefer imperative style. Is it easy enough to do this from codebehind?
P.S. I set extreme values for emphasized item just for testing :)
Upvotes: 1
Views: 1878
Reputation: 11051
Remeber you need to raise the NowPlaying property, by implementing the INotifyPropertyChanged interface? Without that, the binding engine can't see that the property was changed, therefore no update can be made to the view.
And just a small addition: I would definitely favor the first approach, try to avoid datatrigger, in fact all business data related databinding in styles as much as possible. The Style is supposed to contain only the view design, the DataTemplates are used to display real data. If you keep this seperated its much easier to reuse a style.
Upvotes: 4