Я новичок в мире WPF . Мне действительно нужна помощь. Я пытаюсь связать ObservableCollection <> с DataGrid, но он не работает. Я хочу связать, используя только XAML ItemSource = "{Binding}" . Я делаю все это в MVVM .
И еще одна вещь, как View будет подключаться к ViewModel? Есть ли какие-либо изменения, которые я должен сделать в моем файле View XAML.
Можете ли вы предложить мне, откуда я могу полностью изучить WPF.
Имя проекта - MVVMGrid
Модель - Data.cs
public class Data :INotifyPropertyChanged
{
private string _name;
private string _country;
public string Name
{
get { return _name; }
set { _name= value;
OnPropertyChange("Name");
}
}
public string Country
{
get { return _country; }
set { _country= value;
OnPropertyChange("Country");
}
}
protected void OnPropertyChange(string name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
public event PropertyChangedEventHandler PropertyChanged;
}
ViewModel - DataView.cs
public class DataView
{
public DataView()
{
FetchGrid();
}
public static ObservableCollection<Data> FetchGrid()
{
var load = new ObservableCollection<Data>();
load.Add(new Data { Name = "Raja", Country = "INDIA" });
load.Add(new Data { Name = "Ram", Country = "India" });
load.Add(new Data { Name = "Rohan", Country = "USA" });
load.Add(new Data { Name = "Roy", Country = "TURKEY" });
return load;
}
}
Просмотр - MainWindow.xaml
<Window x:Class="MVVMGrid.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Grid"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<DataGrid ItemsSource="{Binding Path= FetchGrid}" AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
<DataGridTextColumn Header="Country" Binding="{Binding Country}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>