第一步:在资源中定义一个居中的样式:
<Window.Resources> <Style x:Key="ListViewItemStyle" TargetType="{x:Type ListViewItem}"> <Setter Property="HorizontalContentAlignment" Value="Stretch"/> </Style> </Window.Resources>
第二步:把ListView中的样式绑定成这个样式:
<ListView x:Name="lv" ItemContainerStyle="{StaticResource ResourceKey=ListViewItemStyle}"> <ListView.View> <GridView> <GridViewColumn Header="序号"> <GridViewColumn.CellTemplate> <DataTemplate> <TextBlock Margin="5" Text="{Binding Path=SeqNo}" TextWrapping="Wrap" FontSize="14" HorizontalAlignment="Center"/> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> <GridViewColumn Header="商品名称"> <GridViewColumn.CellTemplate> <DataTemplate> <TextBlock Margin="5" Text="{Binding Path=GoodsName}" TextWrapping="Wrap" FontSize="14" HorizontalAlignment="Center"/> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> <GridViewColumn Header="操作" Width="100"> <GridViewColumn.CellTemplate> <DataTemplate> <CheckBox IsChecked="{Binding Path=Cnk}" IsThreeState= "true" HorizontalAlignment="Center"/> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> </GridView> </ListView.View> </ListView>
注意:把CheckBox的HorizontalAlignment属性设置为HorizontalAlignment="Center";
运行效果图:
demo程序完整代码如下:
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow"> <Window.Resources> <Style x:Key="ListViewItemStyle" TargetType="{x:Type ListViewItem}"> <Setter Property="HorizontalContentAlignment" Value="Stretch"/> </Style> </Window.Resources> <Grid> <ListView x:Name="lv" ItemContainerStyle="{StaticResource ResourceKey=ListViewItemStyle}"> <ListView.View> <GridView> <GridViewColumn Header="序号"> <GridViewColumn.CellTemplate> <DataTemplate> <TextBlock Margin="5" Text="{Binding Path=SeqNo}" TextWrapping="Wrap" FontSize="14" HorizontalAlignment="Center"/> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> <GridViewColumn Header="商品名称"> <GridViewColumn.CellTemplate> <DataTemplate> <TextBlock Margin="5" Text="{Binding Path=GoodsName}" TextWrapping="Wrap" FontSize="14" HorizontalAlignment="Center"/> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> <GridViewColumn Header="操作" Width="100"> <GridViewColumn.CellTemplate> <DataTemplate> <CheckBox IsChecked="{Binding Path=Cnk}" IsThreeState= "true" HorizontalAlignment="Center"/> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> </GridView> </ListView.View> </ListView> </Grid> </Window>
C#代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Collections.ObjectModel; namespace WpfApplication1 { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { private ObservableCollection<Goods> _goodsList; public ObservableCollection<Goods> GoodsList { get { if (_goodsList == null) { _goodsList = new ObservableCollection<Goods>(); } return _goodsList; } } public MainWindow() { InitializeComponent(); List<Goods> list = new List<Goods> { new Goods{SeqNo=1,GoodsName="苹果",Cnk=true}, new Goods{SeqNo=2,GoodsName="橘子",Cnk=false}, new Goods{SeqNo=3,GoodsName="香蕉",Cnk=false}, new Goods{SeqNo=4,GoodsName="葡萄",Cnk=true}, new Goods{SeqNo=5,GoodsName="哈密瓜",Cnk=true}, }; GoodsList.Clear(); foreach (Goods item in list) { GoodsList.Add(item); } lv.ItemsSource = GoodsList; } } public class Goods { private int _seqNo; public int SeqNo { get { return _seqNo; } set { _seqNo = value; } } private string _goodsName; public string GoodsName { get { return _goodsName; } set { _goodsName = value; } } private bool _cnk; public bool Cnk { get { return _cnk; } set { _cnk = value; } } } }
时间: 2024-11-06 17:49:00