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

  • 0
    点赞
  • 0
    评论
  • 2
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

打赏
文章很值,打赏犒劳作者一下
相关推荐
©️2020 CSDN 皮肤主题: 游动-白 设计师:白松林 返回首页

打赏

manlbj

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者