WPF DataGrid MVVM模式数据绑定

WPF DataGrid MVVM模式数据绑定

1、创建Model

public class User
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public char Sex { get; set; }
        public int Age { get; set; }
        public string Memo { get; set; }
    }

2、创建ViewModel

ViewModelUtil 类用来实现INotifyPropertyChanged接口,UserViewModel类继承ViewModelUtil 添加数据

public class ViewModelUtil : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public void RaisePropertyChanged(string propertyName)
        {
            if (propertyName != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
 public class UserViewModel : ViewModelUtil
    {
        //数据源
        ObservableCollection<User> userData = new ObservableCollection<User>();
        public ObservableCollection<User> UserData
        {

            get { return userData; }
            set
            {
                UserData = value;
                RaisePropertyChanged("mylist");
            }
        }
        public UserViewModel()
        {
            userData.Add(new User() { Id = 1,Name="盖伦",Age=25,Sex='男',Memo = "德玛西亚之力" });
            userData.Add(new User() { Id = 2,Name="伊泽瑞尔",Age=20,Sex='男',Memo = "冒险家" });
            userData.Add(new User() { Id = 3,Name="阿卡丽",Age=20,Sex='女',Memo = "忍者" });
            userData.Add(new User() { Id = 4,Name="亚托克斯",Age=500,Sex='男',Memo = "暗裔,恶魔" });
            userData.Add(new User() { Id = 5,Name="亚索",Age=25,Sex='男',Memo = "疾风剑豪" });
        }

    }

3、创建View

View在xaml中定义,通过数据绑定显示数据。

    <Window.Resources>
        <ResourceDictionary>
            <Style x:Key="dgStyle" TargetType="TextBlock">
                <Setter Property="TextAlignment" Value="Center"/>
                <Setter Property="VerticalAlignment" Value="Center"/>
            </Style>
        </ResourceDictionary>
    </Window.Resources>
    <Grid>
        <DataGrid x:Name="dgData" AutoGenerateColumns="False" CanUserAddRows="False" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0,50,0,0" ItemsSource="{Binding UserData}" >
            <DataGrid.Columns>
                <DataGridTextColumn Header="编号" Binding="{Binding Id}" Width="*" ElementStyle="{StaticResource dgStyle}"/>
                <DataGridTextColumn Header="姓名" Binding="{Binding Name}" Width="*" ElementStyle="{StaticResource dgStyle}"/>
                <DataGridTextColumn Header="性别" Binding="{Binding Sex}" Width="*" ElementStyle="{StaticResource dgStyle}"/>
                <DataGridTextColumn Header="年龄" Binding="{Binding Age}" Width="*" ElementStyle="{StaticResource dgStyle}"/>
                <DataGridTextColumn Header="备注" Binding="{Binding Memo}" Width="*" ElementStyle="{StaticResource dgStyle}"/>
            </DataGrid.Columns>
            <DataGrid.ColumnHeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="HorizontalContentAlignment" Value="Center"/>
                    <Setter Property="FontSize" Value="15"/>
                    <Setter Property="Height" Value="30"/>
                    <Setter Property="Foreground" Value="#1F9845"/>
                    <Setter Property="AllowDrop" Value="False"/>
                </Style>
            </DataGrid.ColumnHeaderStyle>
            <DataGrid.RowStyle>
                <Style TargetType="DataGridRow">
                    <Setter Property="HorizontalContentAlignment" Value="Center"/>
                    <Setter Property="FontSize" Value="13"/>
                    <Setter Property="Height" Value="23"/>
                    <Setter Property="BorderBrush" Value="Gray"/>
                    <Setter Property="BorderThickness" Value="0"/>
                </Style>
            </DataGrid.RowStyle>
        </DataGrid>
    </Grid>

4、数据绑定

 public partial class DataGridWindow : Window
    {
        public DataGridWindow()
        {
            InitializeComponent();
            dgData.DataContext = new UserViewModel();
        }
    }

  • 9
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值