欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

WPF中使用DataGrid时操作列按钮问题

程序员文章站 2022-04-09 15:32:41
原文:WPF中使用DataGrid时操作列按钮问题

 

原文:wpf中使用datagrid时操作列按钮问题

 

 

    <datagrid x:name="datagrid" autogeneratecolumns="false" scrollviewer.horizontalscrollbarvisibility="hidden" sizechanged="datagrid_sizechanged" rowheaderwidth="0" isreadonly="true" borderbrush="transparent" borderthickness="1">
        <datagrid.columnheaderstyle>
            <style targettype="datagridcolumnheader">
                <setter property="horizontalcontentalignment" value="center"></setter>
                <setter property="fontsize" value="20"></setter>
                <setter property="fontweight" value="extrabold"></setter>
                <setter property="height" value="50"></setter>
            </style>
        </datagrid.columnheaderstyle>
 
        <datagrid.columns>
            <datagridtextcolumn x:name="username" binding="{binding name}" header="姓名" fontsize="20"/>
            <datagridtextcolumn x:name="usersex" binding="{binding sex}" header="性别" fontsize="20"/>
            <datagridtextcolumn x:name="userage" binding="{binding age}" header="是否完成" fontsize="20"/>
            <datagridtextcolumn x:name="userphone" binding="{binding phone}" header="下发时间" fontsize="20"/>
            <datagridtemplatecolumn x:name="useraction" header="操作" width="100">
                <datagridtemplatecolumn.celltemplate>
                    <datatemplate>
                        <stackpanel orientation="horizontal">
                            <button x:name="btnaction" content="{binding btnactionstr}" height="34"
                                    width="80" click="btnaction_click" isenabled="{binding enabled}"
                                    fontsize="20"  horizontalcontentalignment="center" verticalcontentalignment="center">
                            </button>
                            <button x:name="btnaction1" content="{binding btnactionstr1}" height="34"
                                    width="80" click="btnaction1_click" isenabled="{binding enabled1}"
                                    fontsize="20"  horizontalcontentalignment="center" verticalcontentalignment="center">
                            </button>
                        </stackpanel>
                    </datatemplate>
                </datagridtemplatecolumn.celltemplate>
            </datagridtemplatecolumn>
        </datagrid.columns>
    </datagrid>
 
 
后台代码
 
list<user> users = new list<user>();
//向datagrid中添加数据
private void getdatagrid()
{
    for (int i = 0; i < 10; i++)
    {
        user user = new user();
        user.name = "tom"; user.sex = "男"; user.age = "18"; user.phone = "000000";
        user.btnactionstr = "按钮" + i;
        user.btnactionstr1 = "按钮" + (i + 1);
        if (i % 2 == 0)
        {
            user.enabled = true;
            user.enabled1 = false;
        }
        else
        {
            user.enabled = false;
            user.enabled1 = true;
        }
        users.add(user);
    }
    //数据绑定
    datagrid.itemssource = users;
}
//定义要绑定的类
private class user
{
    public string name { getset; }
    public string sex { getset; }
    public string age { getset; }
    public string phone { getset; }
    public string btnactionstr { getset; }
    public bool enabled { getset; }
    public string btnactionstr1 { getset; }
    public bool enabled1 { getset; }
}
//平均分配各列的宽度
private void datagrid_sizechanged(object sender, sizechangedeventargs e)
{
    int widthsize = (int)(datagrid.actualwidth / 5 - 4);
    username.width = widthsize; usersex.width = widthsize; userage.width = widthsize;
    userphone.width = widthsize; useraction.width = widthsize;
}
//第一个按钮点击事件
private void btnaction_click(object sender, routedeventargs e)
{
    messagebox.show(users[datagrid.selectedindex].name);
}
//第二个按钮点击事件
private void btnaction1_click(object sender, routedeventargs e)
{
    messagebox.show(users[datagrid.selectedindex].sex);
}
 

 调用:

1
getdatagrid();

 效果图:

WPF中使用DataGrid时操作列按钮问题

WPF中使用DataGrid时操作列按钮问题

WPF中使用DataGrid时操作列按钮问题

勿忘初心,方得始终。