WPF C# Data-binding to DataGridTextColumn WPF C# Data-binding to DataGridTextColumn wpf wpf

WPF C# Data-binding to DataGridTextColumn


myWindow w = new myWindow();         w.DataContext = myViewModel;         w.Show();DataContext="{Binding RelativeSource={RelativeSource AncestorType=Window}}"

OR

<Window x:Name="MyWindow" />    //DataGrid    {Binding DataContext, ElementName=MyWindow}    ItemsSource="{Binding MyViewModel.MyList}"


I would suggest to use the ICollectionView interface im my Windowor better in my modelview class

Here is a example MainWindow.xaml.cs class, which demonstrates how to add data to such interface Class in via code-behind:

using System.Collections.Generic;using System.ComponentModel;using System.Windows;using System.Windows.Data;using System.Windows.Input;namespace WpfAppTest{    public partial class MainWindow : Window    {        public ICollectionView MyMVVMEmployeeList { get; private set; }        public MainWindow()        {            InitializeComponent();        }        private void Button_Click(object sender, RoutedEventArgs e)        {            List<MVVMEmployee> list = new List<MVVMEmployee>();            list.Add(new MVVMEmployee(23, "foomaster", "dr", "busy"));            list.Add(new MVVMEmployee(42, "author", "mister", "dead"));            MyMVVMEmployeeList = CollectionViewSource.GetDefaultView(list);            //we call update gui //not needed when using modelview in pure mvvm             DataContext = this;         }    }}

The Binding in the MainWindow.xaml should then looks like:

<Window x:Class="WpfAppTest.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:WpfAppTest"        mc:Ignorable="d"        DataContext="MainWindow"        Title="MainWindow" Height="450" Width="800">    <!-- for mvvm pattern using the model for datacontext deps e.g. like -->    <!--  DataContext="{Binding Main,Source={StaticResource Locator}}"> -->    <Grid>        <StackPanel Orientation="Vertical">            <DataGrid x:Name="DataGridEmployees"                    ItemsSource="{Binding MyMVVMEmployeeList }"                    AutoGenerateColumns="False">                <DataGrid.Columns>                    <DataGridTextColumn Header="EmployeeId" Width="175" Binding="{Binding Id}" ></DataGridTextColumn>                    <DataGridTextColumn Header="Title" Width="175" Binding="{Binding Title}"></DataGridTextColumn>                    <DataGridTextColumn Header="WorkStatus" Width="175" Binding="{Binding WorkStatus}"></DataGridTextColumn>                    <DataGridTextColumn Header="FullName" Width="175" Binding="{Binding FullName}"></DataGridTextColumn>                </DataGrid.Columns>            </DataGrid>            <Button Content="add" HorizontalAlignment="Left" Click="Button_Click"/>        </StackPanel>    </Grid></Window>

To make it fully runnable here your data class MVVMEmployee.cs again without refereing the base class:

namespace WpfAppTest{    public class MVVMEmployee     {        public int Id { get; set; }        public string FullName { get; set; }        public string Title { get; set; }        public string WorkStatus { get; set; }        public MVVMEmployee() { }        public MVVMEmployee(int id, string fullName, string title, string workStatus)        {            this.Id = id;            this.FullName = fullName;            this.Title = title;            this.WorkStatus = workStatus;        }    }}

Please change the namepspace WpfAppTestwhile including the example above.I would also suggest not use the code behindand use the MyMVVMEmployeeList the same way in your modelview classto follow more the mvvm pattern.