1

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

4

1 回答 1

4

还记得您需要通过实现INotifyPropertyChanged接口来提高 NowPlaying 属性吗?否则,绑定引擎无法看到属性已更改,因此无法对视图进行更新。

并且只是一个小补充:我肯定会赞成第一种方法,尽量避免使用 datatrigger,实际上所有与业务数据相关的数据绑定都尽可能采用样式。Style 应该只包含视图设计,DataTemplates 用于显示真实数据。如果您将其分开,则重用样式会容易得多。

于 2013-03-26T09:27:42.740 回答