我创建了一个" ListBox"并设置" HorizontalContentAlignment"属于" Strecth"。 但是,有时候,当滚动时," HorizontalContentAlignment"不工作 我不明白问题的原因是什么。
我的代码:
<ListBox
Name="ListBoxTracks"
Grid.Row="0"
HorizontalContentAlignment="Stretch"
ItemsSource="{Binding MusicManager.TracksCollection, IsAsync=True}"
ScrollViewer.CanContentScroll="True"
ScrollViewer.IsDeferredScrollingEnabled="True"
SelectedItem="{Binding MusicManager.CurrentTrack, Mode=OneWayToSource}"
VirtualizingPanel.CacheLength="1,1"
VirtualizingPanel.IsVirtualizingWhenGrouping="True"
VirtualizingStackPanel.IsVirtualizing="True"
VirtualizingStackPanel.VirtualizationMode="Recycling">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding MusicManager.GetTracksCollectionCommand}" CommandParameter="{Binding Path=ItemsSource, RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}" />
</i:EventTrigger>
<i:EventTrigger EventName="PreviewMouseDoubleClick">
<i:InvokeCommandAction Command="{Binding MusicManager.InitPlayCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type playable:LocalPlayable}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<fa:ImageAwesome
Grid.Row="0"
Grid.RowSpan="2"
Grid.Column="0"
Width="16"
Height="16"
Margin="5"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Foreground="{DynamicResource PrimaryHueMidForegroundBrush}"
Icon="Music" />
<TextBlock
Grid.Row="0"
Grid.Column="1"
Grid.ColumnSpan="2"
Margin="5,5,5,0"
HorizontalAlignment="Left"
FontSize="12"
TextTrimming="WordEllipsis">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} - {1}">
<Binding Path="Artist[0].Name" />
<Binding Path="Title" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
<TextBlock
Grid.Row="0"
Grid.Column="3"
Margin="5,5,5,0"
HorizontalAlignment="Right"
FontSize="12">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0:00}:{1:00}">
<Binding Path="Duration.Minutes" />
<Binding Path="Duration.Seconds" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
<TextBlock
Grid.Row="1"
Grid.Column="1"
Grid.ColumnSpan="2"
Margin="5,0,5,0"
HorizontalAlignment="Left"
FontSize="12"
Text="{Binding Converter={StaticResource TrackAttributesConverter}}" />
<materialDesign:RatingBar
Grid.Row="1"
Grid.Column="3"
Margin="5,0,5,0"
HorizontalAlignment="Right"
Max="5" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<Grid>
<TextBlock
Margin="15,5,15,5"
HorizontalAlignment="Left"
Foreground="{DynamicResource MaterialDesignBody}"
FontSize="16"
Text="{Binding Path=Name}" />
<TextBlock
Margin="15,5,15,5"
HorizontalAlignment="Right"
Foreground="{DynamicResource MaterialDesignBody}"
FontSize="16"
Text="{Binding Path=ItemCount, StringFormat=Songs:{0:F0}}" />
</Grid>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Rectangle
Grid.Row="0"
Height="1"
HorizontalAlignment="Stretch"
Stroke="{DynamicResource MaterialDesignBody}" />
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="1" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid Grid.Column="0" Width="120">
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<myControls:ImageProviderViewer
Grid.Row="0"
Width="120"
Height="120"
VerticalAlignment="Top"
HidePlaceholderAtBeginning="True"
HighPriorityImage="True"
ImageProvider="{Binding Items[0].Album.Cover}">
<myControls:ImageProviderViewer.Placeholder>
<Viewbox Width="110" Height="110">
<Path Fill="Gray" Data="{StaticResource VectorMicrophone}" />
</Viewbox>
</myControls:ImageProviderViewer.Placeholder>
</myControls:ImageProviderViewer>
<TextBlock
Grid.Row="1"
Margin="2"
FontSize="12"
FontWeight="Medium"
Text="{Binding Items[0].Artist[0].Name}"
TextTrimming="WordEllipsis" />
<TextBlock
Grid.Row="2"
Margin="2"
FontSize="12"
FontWeight="Medium"
Text="{Binding Items[0].Album.Name}"
TextTrimming="WordEllipsis" />
<TextBlock
Grid.Row="3"
Margin="2"
FontSize="12"
FontWeight="Medium"
Text="{Binding Items[0].Album.FirstReleaseDate}"
TextTrimming="WordEllipsis" />
</Grid>
<Rectangle
Grid.Column="1"
Width="1"
VerticalAlignment="Stretch"
Stroke="{DynamicResource MaterialDesignBody}" />
<ItemsPresenter Grid.Column="2" />
</Grid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ListBox.GroupStyle>
</ListBox>
答案 0 :(得分:1)
听起来你想要拉伸ListBoxItems,而不是ListBox本身。
<ListBox>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListBox.ItemContainerStyle>