如何为WPF中的Listview列指定MinWidth
?
答案 0 :(得分:3)
此代码使用Thumb控件。它将阻止用户仅以指定的宽度拖动标题。
在WPF中添加此内容
<ListView x:Name="MyListView"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding Path=Items,
Mode=Default,
Source={StaticResource DataProvider}}"
Thumb.DragDelta="Thumb_DragDelta">
在C#中做
public Window1()
{
InitializeComponent();
MyListView.AddHandler(Thumb.DragDeltaEvent,new DragDeltaEventHandler(Thumb_DragDelta),true);
}
void Thumb_DragDelta(object sender, DragDeltaEventArgs e)
{
Thumb senderAsThumb = e.OriginalSource as Thumb;
GridViewColumnHeader header = senderAsThumb.TemplatedParent as GridViewColumnHeader;
if (header.Column.ActualWidth < MIN_WIDTH)
header.Column.Width = MIN_WIDTH;
if (header.Column.ActualWidth > MAX_WIDTH)
header.Column.Width = MAX_WIDTH;
}