Xaml UWP-使用Stackpanel内容自定义ListBoxItemStyle

Xaml UWP-使用Stackpanel内容自定义ListBoxItemStyle,xaml,listbox,styles,win-universal-app,Xaml,Listbox,Styles,Win Universal App,我正在努力解决UWP-XAML中ListBoxItem的样式概念 我想要实现的是一种列表框样式,其中的项目由两个文本块组成的堆栈面板组成,当按Select时,这两个文本块的前景会发生不同的变化 在下面的ListBox示例中,我只能在标准ListBoxItemStyle中设置ContentPresenter的样式,两个文本块一起更改 <ListBox> <ListBox.ItemTemplate> <DataTemplate x:DataTyp

我正在努力解决UWP-XAML中ListBoxItem的样式概念

我想要实现的是一种列表框样式,其中的项目由两个文本块组成的堆栈面板组成,当按Select时,这两个文本块的前景会发生不同的变化

在下面的ListBox示例中,我只能在标准ListBoxItemStyle中设置ContentPresenter的样式,两个文本块一起更改

<ListBox>
    <ListBox.ItemTemplate>
        <DataTemplate x:DataType="local:DataSet">
            <StackPanel>
                <TextBlock x:Name="number" Text="{x:Bind Number}"/>
                <TextBlock x:Name="name" Text="{x:Bind Name}"/>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

您必须编辑ItemContainer的样式。将ContentPresenter替换为stackpanel及其内容。但是我使用了绑定而不是x:Bind。似乎不可能在样式内部使用x:Bind

<Style TargetType="ListBoxItem" x:Key="ListViewStyle">
            <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}" />
            <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}" />
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="Foreground" Value="{ThemeResource SystemControlForegroundBaseHighBrush}" />
            <Setter Property="TabNavigation" Value="Local"/>
            <Setter Property="IsHoldingEnabled" Value="True"/>
            <Setter Property="Padding" Value="12,0,12,0"/>
            <Setter Property="HorizontalContentAlignment" Value="Left"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="MinWidth" Value="{ThemeResource ListViewItemMinWidth}"/>
            <Setter Property="MinHeight" Value="{ThemeResource ListViewItemMinHeight}"/>
            <Setter Property="UseSystemFocusVisuals" Value="True" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBoxItem">
                        <Grid x:Name="ContentBorder"
              Background="{TemplateBinding Background}"
              BorderBrush="{TemplateBinding BorderBrush}"
              BorderThickness="{TemplateBinding BorderThickness}">
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Normal">
                                    </VisualState>
                                    <VisualState x:Name="PointerOver">
                                    </VisualState>
                                    <VisualState x:Name="Pressed">
                                    </VisualState>
                                    <VisualState x:Name="Selected">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="Red" />
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter1" Storyboard.TargetProperty="Foreground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="Yellow" />
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="SelectedPointerOver">
                                    </VisualState>
                                    <VisualState x:Name="SelectedUnfocused">
                                        <Storyboard>

                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="Red" />
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter1" Storyboard.TargetProperty="Foreground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="Yellow" />
                                            </ObjectAnimationUsingKeyFrames>

                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="SelectedPressed">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="Red" />
                                                </ObjectAnimationUsingKeyFrames>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter1" Storyboard.TargetProperty="Foreground">
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="Yellow" />
                                                </ObjectAnimationUsingKeyFrames>

                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                                <VisualStateGroup x:Name="DisabledStates">
                                    <VisualState x:Name="Enabled"/>
                                    <VisualState x:Name="Disabled">
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <Rectangle x:Name="BorderBackground"
                    IsHitTestVisible="False"
                    Fill="{ThemeResource SystemControlHighlightListAccentLowBrush}"
                    Opacity="0"
                    Control.IsTemplateFocusTarget="True"/>
                            <StackPanel  Background="Transparent"
              Margin="0,0,0,0">
                                <TextBlock x:Name="ContentPresenter" Text="{Binding Number}"/>
                                <TextBlock Grid.Column="1" x:Name="ContentPresenter1" Text="{Binding Name}"/>
                            </StackPanel>

                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>



<ListBox  ItemContainerStyle="{StaticResource ListViewStyle}" x:Name="listview">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

效果很好,感谢您解决了这一困惑。