【问题标题】:WP8: Change the Listboxitem Datatemplate on Selection ChangedWP8:更改选择时的 Listboxitem 数据模板已更改
【发布时间】:2015-03-25 06:57:46
【问题描述】:

我在 windows phone 8 中有一个列表框,

为此,我想更改所选项目的数据模板。

我已经这样做了,就像 WPF 一样:

<Window.Resources>

        <DataTemplate x:Key="ItemTemplate">
            <TextBlock Text="{Binding}" Foreground="Red" />
        </DataTemplate>

        <DataTemplate x:Key="SelectedTemplate">
            <TextBlock Text="{Binding}" Foreground="Green" />
        </DataTemplate>

        <Style TargetType="{x:Type ListBoxItem}" x:Key="ContainerStyle">
            <Setter Property="ContentTemplate" Value="{StaticResource ItemTemplate}" />
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="ContentTemplate" Value="{StaticResource SelectedTemplate}" />
                </Trigger>
            </Style.Triggers>
        </Style>

    </Window.Resources>
    <ListBox x:Name="lstItems" ItemContainerStyle="{StaticResource ContainerStyle}" />

但我不知道如何在 Windows Phone 8 中实现这一点,因为它没有

支持Trigggers

【问题讨论】:

    标签: c# xaml windows-phone-8 listbox windows-phone-8.1


    【解决方案1】:

    在 C# 中您可以轻松使用

    ListBox.ItemTemplate = (DataTemplate)this.Resources["yourkeytemplate"];

    不要忘记在资源 XAML 中声明您的模板

    【讨论】:

      【解决方案2】:

      我在努力,我成功了。

      我们可以为此使用VisualStateManager

      <Page.Resources>
      <DataTemplate x:Key="ItemTemplate">
                  <TextBlock Text="{Binding}" Foreground="Blue" FontSize="20"/>
              </DataTemplate>
      
              <DataTemplate x:Key="SelectedTemplate">
                  <TextBlock Text="{Binding}" Foreground="Red" FontSize="20"/>
              </DataTemplate>
      
      <Style TargetType="ListBoxItem" x:Key="custom">
                  <Setter Property="Background" Value="Transparent" />
                  <Setter Property="TabNavigation" Value="Local" />
                  <Setter Property="Padding" Value="8,10" />
                  <Setter Property="HorizontalContentAlignment" Value="Left" />
                  <Setter Property="Template">
                      <Setter.Value>
                          <ControlTemplate TargetType="ListBoxItem">
                              <Border x:Name="LayoutRoot"
                              Background="{TemplateBinding Background}"
                              BorderBrush="{TemplateBinding BorderBrush}"
                              BorderThickness="{TemplateBinding BorderThickness}">
                                  <VisualStateManager.VisualStateGroups>
                                      <VisualStateGroup x:Name="CommonStates">
                                          <VisualState x:Name="Normal" />
                                          <VisualState x:Name="PointerOver">
                                              <Storyboard>
                                                  <ObjectAnimationUsingKeyFrames Storyboard.TargetName="LayoutRoot"
                                                                         Storyboard.TargetProperty="Background">
                                                      <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ListBoxItemPointerOverBackgroundThemeBrush}" />
                                                  </ObjectAnimationUsingKeyFrames>
                                                  <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                                                         Storyboard.TargetProperty="Foreground">
                                                      <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ListBoxItemPointerOverForegroundThemeBrush}" />
                                                  </ObjectAnimationUsingKeyFrames>
                                              </Storyboard>
                                          </VisualState>
                                          <VisualState x:Name="Disabled">
                                              <Storyboard>
                                                  <ObjectAnimationUsingKeyFrames Storyboard.TargetName="LayoutRoot"
                                                                         Storyboard.TargetProperty="Background">
                                                      <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent" />
                                                  </ObjectAnimationUsingKeyFrames>
                                                  <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                                                         Storyboard.TargetProperty="Foreground">
                                                      <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ListBoxItemDisabledForegroundThemeBrush}" />
                                                  </ObjectAnimationUsingKeyFrames>
                                              </Storyboard>
                                          </VisualState>
                                          <VisualState x:Name="Pressed">
                                              <Storyboard>
                                                  <DoubleAnimation Storyboard.TargetName="PressedBackground"
                                                           Storyboard.TargetProperty="Opacity"
                                                           To="1"
                                                           Duration="0" />
                                                  <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                                                         Storyboard.TargetProperty="Foreground">
                                                      <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ListBoxItemPressedForegroundThemeBrush}" />
                                                  </ObjectAnimationUsingKeyFrames>
                                              </Storyboard>
                                          </VisualState>
                                      </VisualStateGroup>
                                      <VisualStateGroup x:Name="SelectionStates">
                                          <VisualState x:Name="Unselected" >
                                              <Storyboard>
                                                  <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                                                                 Storyboard.TargetProperty="ContentTemplate">
                                                      <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ItemTemplate}"/>
                                                  </ObjectAnimationUsingKeyFrames>
                                              </Storyboard>
                                          </VisualState>
                                          <VisualState x:Name="Selected">
                                              <Storyboard>
                                                  <ObjectAnimationUsingKeyFrames Storyboard.TargetName="InnerGrid"
                                                                         Storyboard.TargetProperty="Background">
                                                      <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ListBoxItemSelectedBackgroundThemeBrush}" />
                                                  </ObjectAnimationUsingKeyFrames>
                                                  <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                                                         Storyboard.TargetProperty="Foreground">
                                                      <DiscreteObjectKeyFrame KeyTime="0" Value="Red" />
                                                  </ObjectAnimationUsingKeyFrames>
                                                  <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                                                                 Storyboard.TargetProperty="ContentTemplate">
                                                      <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SelectedTemplate}"/>
                                                  </ObjectAnimationUsingKeyFrames>
                                              </Storyboard>
                                          </VisualState>
                                          <VisualState x:Name="SelectedUnfocused">
                                              <Storyboard>
                                                  <ObjectAnimationUsingKeyFrames Storyboard.TargetName="InnerGrid"
                                                                         Storyboard.TargetProperty="Background">
                                                      <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ListBoxItemSelectedBackgroundThemeBrush}" />
                                                  </ObjectAnimationUsingKeyFrames>
                                                  <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                                                         Storyboard.TargetProperty="Foreground">
                                                      <DiscreteObjectKeyFrame KeyTime="0" Value="Red" />
                                                  </ObjectAnimationUsingKeyFrames>
                                                  <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                                                                 Storyboard.TargetProperty="ContentTemplate">
                                                      <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SelectedTemplate}"/> 
                                                  </ObjectAnimationUsingKeyFrames>
                                              </Storyboard>
                                          </VisualState>
                                          <VisualState x:Name="SelectedDisabled">
                                              <Storyboard>
                                                  <ObjectAnimationUsingKeyFrames Storyboard.TargetName="InnerGrid"
                                                                         Storyboard.TargetProperty="Background">
                                                      <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ListBoxItemSelectedDisabledBackgroundThemeBrush}" />
                                                  </ObjectAnimationUsingKeyFrames>
                                                  <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                                                         Storyboard.TargetProperty="Foreground">
                                                      <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ListBoxItemSelectedDisabledForegroundThemeBrush}" />
                                                  </ObjectAnimationUsingKeyFrames>
                                                  <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                                                                 Storyboard.TargetProperty="ContentTemplate">
                                                      <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SelectedTemplate}"/>
                                                  </ObjectAnimationUsingKeyFrames>
                                              </Storyboard>
                                          </VisualState>
                                          <VisualState x:Name="SelectedPointerOver">
                                              <Storyboard>
                                                  <ObjectAnimationUsingKeyFrames Storyboard.TargetName="InnerGrid"
                                                                         Storyboard.TargetProperty="Background">
                                                      <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ListBoxItemSelectedPointerOverBackgroundThemeBrush}" />
                                                  </ObjectAnimationUsingKeyFrames>
                                                  <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                                                         Storyboard.TargetProperty="Foreground">
                                                      <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ListBoxItemSelectedForegroundThemeBrush}" />
                                                  </ObjectAnimationUsingKeyFrames>
                                                  <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                                                                 Storyboard.TargetProperty="ContentTemplate">
                                                      <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SelectedTemplate}"/>
                                                  </ObjectAnimationUsingKeyFrames>
                                              </Storyboard>
                                          </VisualState>
                                          <VisualState x:Name="SelectedPressed">
                                              <Storyboard>
                                                  <ObjectAnimationUsingKeyFrames Storyboard.TargetName="InnerGrid"
                                                                         Storyboard.TargetProperty="Background">
                                                      <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ListBoxItemSelectedBackgroundThemeBrush}" />
                                                  </ObjectAnimationUsingKeyFrames>
                                                  <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                                                         Storyboard.TargetProperty="Foreground">
                                                      <DiscreteObjectKeyFrame KeyTime="0" Value="Green" />
                                                  </ObjectAnimationUsingKeyFrames>
                                                  <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                                                                 Storyboard.TargetProperty="ContentTemplate">
                                                      <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SelectedTemplate}"/>
                                                  </ObjectAnimationUsingKeyFrames>
                                              </Storyboard>
                                          </VisualState>
                                      </VisualStateGroup>
                                      <VisualStateGroup x:Name="FocusStates">
                                          <VisualState x:Name="Focused">
                                              <Storyboard>
                                                  <DoubleAnimation Storyboard.TargetName="FocusVisualWhite"
                                                           Storyboard.TargetProperty="Opacity"
                                                           To="1"
                                                           Duration="0" />
                                                  <DoubleAnimation Storyboard.TargetName="FocusVisualBlack"
                                                           Storyboard.TargetProperty="Opacity"
                                                           To="1"
                                                           Duration="0" />
                                              </Storyboard>
                                          </VisualState>
                                          <VisualState x:Name="Unfocused" />
                                          <VisualState x:Name="PointerFocused" />
                                      </VisualStateGroup>
                                  </VisualStateManager.VisualStateGroups>
                                  <Grid x:Name="InnerGrid"
                                Background="Transparent" >
                                      <Rectangle x:Name="PressedBackground"
                                         Fill="{ThemeResource ListBoxItemPressedBackgroundThemeBrush}"
                                         Opacity="0" />
                                      <ContentPresenter x:Name="ContentPresenter"
                                                Content="{TemplateBinding Content}"
                                                ContentTransitions="{TemplateBinding ContentTransitions}"
                                                ContentTemplate="{TemplateBinding ContentTemplate}"
                                                HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                                VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                                Margin="{TemplateBinding Padding}" />
                                      <Rectangle x:Name="FocusVisualWhite"
                                         Stroke="{ThemeResource FocusVisualWhiteStrokeThemeBrush}"
                                         StrokeEndLineCap="Square"
                                         StrokeDashArray="1,1"
                                         Opacity="0"
                                         StrokeDashOffset=".5" />
                                      <Rectangle x:Name="FocusVisualBlack"
                                         Stroke="{ThemeResource FocusVisualBlackStrokeThemeBrush}"
                                         StrokeEndLineCap="Square"
                                         StrokeDashArray="1,1"
                                         Opacity="0"
                                         StrokeDashOffset="1.5" />
                                  </Grid>
                              </Border>
                          </ControlTemplate>
                      </Setter.Value>
                  </Setter>
              </Style>
      <Grid>
              <ListBox x:Name="lstItems" 
                       ItemContainerStyle="{StaticResource custom}"/>
      </Grid>
      

      对不起,它有点长,但我认为它可能对某人有所帮助! :)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-07
        • 1970-01-01
        • 2019-03-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多