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

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

运行效果图:

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

WPF之让ListView中的CheckBox居中显示的更多相关文章

  1. 【转】listView中,checkBox的显示和隐藏

    原文网址:http://www.cnblogs.com/vicma/p/3460500.html 在listView中,每个item都有一个ChexBox,当显示的时候在listView外面设置一个按 ...

  2. 用WPF实现在ListView中的鼠标悬停Tooltip显示

    原文:用WPF实现在ListView中的鼠标悬停Tooltip显示 一.具体需求描述 在WPF下实现,当鼠标悬停在ListView中的某一元素的时候能弹出一个ToolTip以显示需要的信息. 二.代码 ...

  3. java中关于窗体居中显示问题

    最近在学着用java写qq聊天程序,首先是登录和聊天界面,书上没有给出居中显示,通过上网查找知道有两种方案 先说第一种方法,也是以前用的方法 /获得屏幕大小Dimension screenSize = ...

  4. 设置div中的div居中显示

    设置div中的div居中显示 方法一. <div class='big'> <div class='small'>box1</div> </div> s ...

  5. WPF: 在ListView中添加Checkbox列表

    描述:ListView是WPF中动态绑定工具的数据容器,本文实现了一个在ListView中显示的供用户选择的列表项目,并且控制列表中选择的项目数量,即实现单选. XAML中创建ListView,代码如 ...

  6. Android listview中使用checkbox

    最近比较忙碌,我也不知道忙的什么东西,打算写的博客写了一半,还没写完,今天先扯一扯项目中遇到的一个问题,一方面防止以后遇到这个问题忘记如何解决,另一方面希望可以提供给遇到同样问题的朋友一个思路.下面开 ...

  7. Android ListView 中的checkbox

    Q:ListView + CheckBox 当上下滚动的时候有事会自动选中或取消 A:这个与ListView的缓存机制有关.当你屏幕滚动后,ListView中的item选项视图先检查缓存中是否有视图, ...

  8. 如何在Android的ListView中构建CheckBox和RadioButton列表(支持单选和多选的投票项目示例)

    引言 我们在android的APP开发中有时候会碰到提供一个选项列表供用户选择的需求,如在投票类型的项目中,我们提供一些主题给用户选择,每个主题有若干选项,用户对这些主题的选项进行选择,然后提交. 本 ...

  9. Android ListView 中加入CheckBox/RadioButton 选择状态保持、全选、反选实现

    最近在一个项目中,需要在ListView的item中加入CheckBox,但是遇到的一个问题是上下滑动的时候如果有选择了的CheckBox,就会出现选择项错误的问题,下面将个人的解决方法总结如下;先说 ...

随机推荐

  1. php 基础篇 php 进阶篇

    1:echo 12*3: 2  echo "Welcome" . " to imooc!";字符串连接. 3>foreach($shuzu as $key ...

  2. hdu_5950_Recursive sequence(矩阵快速幂)

    题目链接:hdu_5950_Recursive sequence 题意:递推求解:F(n) = 2*F(n-2) + F(n-1) + n4 和F(1) = a,F(2) = b: 题解: 一看数据范 ...

  3. Caffe+Ubuntu14.04+CUDA7.5 环境搭建(新人向)指南

    序 本文针对想学习使用caffe框架的纯新手,如果文中有错误欢迎大家指出. 由于我在搭建这个环境的时候参考了许多网上的教程,但是没有截图,所以文中图片大多来源于网络. 本文没有安装matlab的步骤, ...

  4. Where is the python library installed?

    configure: error: Could not link test program to Python. Maybe the main Python library has been inst ...

  5. iOS UIScrollview代理方法

    方法&&属性: // 监控目前滚动的位置(默认CGPointZero) CGPoint contentOffset; - (void)setContentOffset:(CGPoint ...

  6. iOS 打电话、发短信、发邮件功能

    打电话 方法1 最简单最直接的方式:直接跳到拨号界面 NSURL *url = [NSURL URLWithString:@"tel://10010"]; [[UIApplicat ...

  7. 用http-server 创建node.js 静态服务器

    今天做一本书上的例子,结果代码不能正常运行,查询了一下,是语法过时了,书其实是新买的,出版不久. 过时代码如下 var connect=require('connect'); connect.crea ...

  8. HDU1007--Quoit Design(平面最近点对)

    Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat rings ...

  9. memcached + php 扩展 for ubuntu

    1.安装memcached apt-get install memcached 2.安装php memcached 扩展 apt-get install php5-memcache 3.启动memca ...

  10. android 属性动画

    一直再追郭霖的博客和imooc上的一些新的视频,最近有讲到属性动画. 以下内容为博客学习以及imooc上视频资料的学习笔记: 在3.0之前比较常见的动画为tween动画和frame动画: tween动 ...