【C#/WPF】ListView的MVVM例子,及禁止拖动ListView的头部Header
一个ListView的MVVM简单例子:
<ListView ItemsSource="{Binding GoodsList}" Margin="0,10,0,10">
<ListView.View>
<GridView>
<GridViewColumn Header="套餐编号" Width="120" DisplayMemberBinding="{Binding packageNo}" />
<GridViewColumn Header="数量" Width="120" DisplayMemberBinding="{Binding num}" />
<GridViewColumn Header="单价" Width="120" DisplayMemberBinding="{Binding price}" />
<GridViewColumn Header="总金额" Width="120" DisplayMemberBinding="{Binding totalAmount}" />
<GridViewColumn Header="选择" Width="120">
<!-- 可以定义Cell的模板 -->
<GridViewColumn.CellTemplate>
<DataTemplate>
<!--<TextBlock Text="{Binding Mail}" TextDecorations="Underline" Foreground="Blue" Cursor="Hand" />-->
<RadioButton GroupName="package" Checked="RadioButton_Checked"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
运行后效果如下(未填充任一条目):
发现问题:用户可以自由拖拽以改变头部的宽度!需要禁止这一操作。
给< GridView >设置它的头部样式为禁用即可,代码如下:
<GridView.ColumnHeaderContainerStyle>
<Style TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="IsEnabled" Value="False"/>
</Style>
</GridView.ColumnHeaderContainerStyle>
重要参考:
(谷歌搜wpf lock listview header)
http://stackoverflow.com/questions/181956/prevent-user-from-resizing-columns-with-wpf-listview