• 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();

     效果图:

    勿忘初心,方得始终。
  • 相关阅读:
    vim的强大,vim设置和插件的使用,脱离windows才是王道
    [VS2013]如何闪开安装VS2013必须要有安装IE10的限制
    常用客户端实现逻辑
    开源控件ViewPagerIndicator学习
    常用设计模式
    主题演讲:未来新趋势电动车
    你最美好的年华
    一度总结
    android线程池ThreadPoolExecutor的理解
    Touch事件or手机卫士面试题整理回答(二)
  • 原文地址:https://www.cnblogs.com/rh-fernando/p/11356990.html
Copyright © 2020-2023  润新知