WPF之让ListView中的CheckBox居中显示

时间:2023-03-08 15:57:07

第一步:在资源中定义一个居中的样式:

   <Window.Resources>
<Style x:Key="ListViewItemStyle" TargetType="{x:Type ListViewItem}">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</Window.Resources>

第二步:把ListView中的ItemContainerStyle样式绑定成这个样式:

   <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";

运行效果图:

WPF之让ListView中的CheckBox居中显示

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=,GoodsName="苹果",Cnk=true},
new Goods{SeqNo=,GoodsName="橘子",Cnk=false},
new Goods{SeqNo=,GoodsName="香蕉",Cnk=false},
new Goods{SeqNo=,GoodsName="葡萄",Cnk=true},
new Goods{SeqNo=,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; }
} }
}