原文地址 https://www.cnblogs.com/younShieh/p/17008534.html
❤如果本文對你有所幫助,不妨點個關注和推薦呀,這是對筆者最大的支援~❤
我需要一個ListBox,他在介面上分為幾列,每列對應一系列的資料。第一行是各資料的標題,支援橫向捲動,豎向只支援資料來源捲動,標題不隨之捲動。視覺上與ListView類似。支援等比拉伸,支援多選,支援從介面去更改內部資料來源,支援子項從ListBox中刪除自己。為了實現這些功能,我決定自定義一個特殊的列表,當然他還是繼承自ListBox。
<Grid.ColumnDefinitions>
<ColumnDefinition Width="74*" />
<ColumnDefinition Width="179*" />
<ColumnDefinition Width="157*" />
</Grid.ColumnDefinitions>
public partial class TestItem : ListBoxItem
{
public TestItem()
{
InitializeComponent();
}
private void Delete_Click(object sender, RoutedEventArgs e)
{
}
}
因為我是繼承自ListBoxItem的,所以可以通過
ItemsControl.ItemsControlFromItemContainer(this) is ListBox listBox
獲取到父listbox的物件,獲取到父物件後,就可以從ListBox中刪除自己。或是更改ListBox中繫結的資料來源。
internal class TestListBox : ListBox
{
protected override DependencyObject GetContainerForItemOverride()
{
return new TestItem();
}
}
<ScrollViewer
Grid.Row="1"
Margin="0,20,0,0"
HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Disabled">
<Border
MinWidth="{Binding MinWidth, ElementName=listbox}"
BorderBrush="#DFDFDF"
BorderThickness="1">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="52" />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="79*" />
<ColumnDefinition Width="104*" />
<ColumnDefinition Width="80*" />
</Grid.ColumnDefinitions>
<Border Grid.ColumnSpan="20" Background="#F2F2F2" />
<TextBlock Text="ID" />
<TextBlock Grid.Column="1" Text="編號" />
<TextBlock Grid.Column="2" Text="操作" />
<controls:TestListBox
x:Name="listbox"
Grid.Row="1"
Grid.ColumnSpan="3"
MinWidth="1100"
ItemsSource="{Binding ItemsSourceData}"
Style="{StaticResource BaseListBoxStyle.WithOutHorizontalScrollViewer}" />
</Grid>
</Border>
</ScrollViewer>
在Listbox的樣式中取消了橫向滑動條的顯示。
<Style x:Key="BaseListBoxStyle.WithOutScrollViewer" TargetType="ListBox">
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
<Setter Property="ScrollViewer.CanContentScroll" Value="False" />
<Setter Property="ScrollViewer.PanningMode" Value="Both" />
<Setter Property="Stylus.IsFlicksEnabled" Value="False" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBox">
<Border
x:Name="Bd"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
SnapsToDevicePixels="true">
<ScrollViewer
Focusable="false"
HorizontalScrollBarVisibility="Disabled"
VerticalScrollBarVisibility="Auto">
<ItemsPresenter Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</ScrollViewer>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>