【问题标题】:In Windows Phone 8.1 how can I change the style of a ListView Item when it is selected在 Windows Phone 8.1 中,如何在 ListView 项目被选中时更改其样式
【发布时间】:2015-05-08 16:15:51
【问题描述】:

我创建了一个 Win Phone 8.1 项目,显示一个 ListView,其中每个项目都显示在一个简单的 TextBlock 中。就像在第一个 HubSection 中使用 Store Apps -> Windows Phone Apps 下的 Hub App 模板创建新项目一样。

<HubSection x:Uid="HubSection1" Header="My Data">
            <DataTemplate>
                <ListView
                    ItemsSource="{Binding CollectionOfData}">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Margin="0,0,0,16">
                                <TextBlock Text="{Binding MyDataProperty}" Style="{ThemeResource ListViewItemTextBlockStyle}" />
                            </StackPanel>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </DataTemplate>
        </HubSection>

现在我想弄清楚如何更改所选 ListViewItem TextBlock 中文本的样式。那就是我希望将所选项目的文本设置为粗体。我想我以前使用触发器做过类似的事情。

【问题讨论】:

    标签: windows-phone-8.1 winrt-xaml


    【解决方案1】:

    我认为您可以使用 Microsoft.Xaml.Interactivity 和 HTML 标记来实现这一点。查看这篇博客文章:Displaying HTML content in a TextBlock。我有一个固定的绑定和样式代码,如果你需要的话,告诉我。

    【讨论】:

      【解决方案2】:

      可以编辑ItemStyleContainer,然后找到VisualState x:Name="Selected"改成这样:

      <VisualState x:Name="Selected">
          <Storyboard>
              <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="CheckGlyph"/>
              <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="SelectedCheckMark"/>
              <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="contentPresenter">
                  <DiscreteObjectKeyFrame KeyTime="0">
                      <DiscreteObjectKeyFrame.Value>
                          <SolidColorBrush Color="Red"/>
                      </DiscreteObjectKeyFrame.Value>
                  </DiscreteObjectKeyFrame>
              </ObjectAnimationUsingKeyFrames>
          </Storyboard>
      </VisualState>
      

      您可以将&lt;SolidColorBrush Color="Red"/&gt; 从红色更改为您想要的颜色

      【讨论】:

        猜你喜欢
        • 2016-11-21
        • 1970-01-01
        • 1970-01-01
        • 2015-07-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多