我有一個ListView如下:
<ListView ItemsSource="{Binding SerialNumbers}" SelectionMode="Multiple" HorizontalAlignment="Stretch">
<ListView.ItemTemplate>
<DataTemplate>
<Border Background="Red" Padding="14 5" HorizontalAlignment="Stretch">
<TextBlock HorizontalAlignment="Stretch" Text="{Binding Number}" />
<behaviours:Interaction.Triggers>
<behaviours:EventTrigger EventName="MouseLeftButtonUp">
<behaviours:InvokeCommandAction Command="{Binding Path=DataContext.SerialNumberSelectedCommand, RelativeSource={RelativeSource AncestorType=UserControl}}" CommandParameter="{Binding}" />
</behaviours:EventTrigger>
</behaviours:Interaction.Triggers>
</Border>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
我希望將Border全寬作為ListViewItem,但它沒有發生。我試過添加HorizontalAlignment="Stretch"到ListView,Border和TextBlock. 它不作業。它目前看起來像這樣。

如何拉伸邊框以獲得ListViewItem.
我想實作這一點,因為我有一個鏈接到的MouseLeftButtonUp事件Border,它將呼叫一個命令。由于Border只有紅色部分,所以只有在點擊紅色部分時才會呼叫。我希望在單擊ListViewItem.
uj5u.com熱心網友回復:
在我看來,實作它的最干凈的方法是設定HorizontalContentAlignment為ListViewItem. 您可以使用樣式輕松實作此目的,如下所示:
<ListView>
<ListView.ItemTemplate>
<DataTemplate>
<Border Background="Red" >
<TextBlock Text="{Binding}"/>
</Border>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
</ListView>
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/492186.html
