zoukankan      html  css  js  c++  java
  • 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";

    运行效果图:

    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
    {
        /// <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; }
            }
            
        }
    }
    View Code
  • 相关阅读:
    json
    [题解]luogu_P2151_HH去散步(矩阵floyd
    [题解]数字计数(数位dp(模板向
    【简单计数知识】JZOJ6395. 【NOIP2019模拟2019.10.28】消失的序列
    字符云例子
    JAVA FileUtils(文件读写以及操作工具类)
    AT2657 Mole and Abandoned Mine
    Problem: [Usaco2018 Open]Team Tic Tac Toe
    Problem: [USACO2018 Jan]Blocked Billboard II
    算法——星星树
  • 原文地址:https://www.cnblogs.com/527289276qq/p/4925675.html
Copyright © 2011-2022 走看看