WPF之让ListView中的CheckBox居中显示

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

   <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>
View Code

 

注意:把CheckBox的HorizontalAlignment属性设置为HorizontalAlignment="Center";

运行效果图:

WPF之让ListView中的CheckBox居中显示_第1张图片

 

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>
View Code

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
{
    /// 
    /// MainWindow.xaml 的交互逻辑
    /// 
    public partial class MainWindow : Window
    {
        private ObservableCollection _goodsList;

        public ObservableCollection GoodsList
        {
            get 
            {
                if (_goodsList == null)
                {
                    _goodsList = new ObservableCollection();
                }
                return _goodsList; 
            }
        }
        

        public MainWindow()
        {
            InitializeComponent();
            List list = new List 
            {
                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; }
        }
        
    }
}
View Code

 

转载于:https://www.cnblogs.com/527289276qq/p/4925675.html

你可能感兴趣的:(c#)