前提・実現したいこと
WPFでMVVMアーキテクチャを利用したプログラミングを進めています。
ViewにDataGridを表示するアプリを作っています。
DataGridにデータが表示されずに困っています。何卒、ご教授をお願いいたします。
発生している問題
データがBindingされておらず(?)表示されなくて困っています。
該当のソースコード
View
Xaml
1<Window x:Class="appwpf1.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:appwpf1" 7 xmlns:vm="clr-namespace:appwpf1.VM" 8 xmlns:md="clr-namespace:appwpf1.Model" 9 mc:Ignorable="d" 10 Title="MainWindow" Height="450" Width="800"> 11 <Window.DataContext> 12 <vm:MainWindowVieModel/> 13 </Window.DataContext> 14 ・ 15 ・ 16 ・ 17<DataGrid x:Name="DataGrid1" ItemsSource="{Binding ParamA}" Grid.Row="1" IsReadOnly="True" AutoGenerateColumns="False" VerticalScrollBarVisibility="Auto" Height="40"> 18 <DataGrid.Columns> 19 <DataGridTextColumn Header="ID" Binding="{Binding ParamA.ID}" /> 20 <DataGridTextColumn Header="Name" Binding="{Binding ParamA.Name}" /> 21 <DataGridTextColumn Header="Sex" Binding="{Binding ParamA.Sex}" /> 22 <DataGridTextColumn Header="メール" Binding="{Binding ParamA.Email}" /> 23 </DataGrid.Columns> 24</DataGrid>
ViewModel
MainWindowVieModel
1 class MainWindowVieModel: Base 2 { 3 public ObservableCollection<ParamA> _observaParam { get; set; } 4 5 private Param_paramA ; 6 public Param ParamA 7 { 8 get 9 { 10 return _paramA ; 11 } 12 set 13 { 14 _paramA = value; 15 NotifyPropertyChange(nameof(ParamA)); 16 } 17 } 18 19 //コンストラクタ 20 public MainWindowVieModel() 21 { 22 ParamA= new ParamA{ ID = 1, Name= "AAA", Sex= "男", Email= "xxx@gmail.com" }; 23 } 24 }
Model
Base
1 class Base: INotifyPropertyChanged 2 { 3 4 public event PropertyChangedEventHandler PropertyChanged; 5 6 protected virtual void NotifyPropertyChange(string propertyName) 7 { 8 if (PropertyChanged != null) 9 { 10 PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 11 } 12 } 13 }
Model
ParamModel
1 class ParamModel 2 { 3 4 5 } 6 7 //パラメーター 8 public class Param 9 { 10 11 public int ID{ get; set; } 12 public string Name{ get; set; } 13 public string Sex{ get; set; } 14 public string Email{ get; set; } 15 }
試したこと
DataGridのMVVM形式での実装は下記を参考にしました
https://www.fenet.jp/dotnet/column/tool/4287/
実装方法が悪いのかと、MVVMについて調査
https://www.atmarkit.co.jp/ait/articles/1011/09/news102.html
補足情報(FW/ツールのバージョンなど)
Visual Studio2019 / .NET Framework4.5.2
Windows10
回答1件
あなたの回答
tips
プレビュー