Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am making a Address Book program in WPF and i want to bind my DataGridColumn from a text file having contacts information.
But, i dont know how to do it. Please Anyone Help Me...

Here's my ViewModel(ContactsViewModel.cs):

C#
using Caliburn.Micro;
using System.Windows;

namespace AddressBook {
public class ContactsViewModel : Screen {
   //What to do here ?
}
}


Here's the XAML Code(ContactsView.xaml):

<UserControl x:Class="AddressBook.ContactsView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <DataGrid FontSize="16" CanUserResizeColumns="False" SelectedIndex="1">
        <DataGrid.Columns>
            <DataGridTextColumn FontSize="14" Header="Name" Width="150"/>
            <DataGridTextColumn FontSize="14" Header="Mobile No." Width="170"/>
            <DataGridTextColumn FontSize="14" Header="Email" Width="200"/>
            <DataGridTextColumn FontSize="14" Header="Address" Width="240"/>
        </DataGrid.Columns>
    </DataGrid>

</UserControl>
Posted
Updated 6-Apr-18 1:05am
v2
Comments
Richard MacCutchan 26-Apr-15 3:10am    
You need to split the fields of your text file so the individual items can be bound. Search the articles section for samples of data binding.

hi . if you want to use databinding in wpf , your model class implement INotifyPropertyChanged. see following:
C#
public class ContactsViewModel : INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, e);
        }
    }

    private string _firstName;
    public string FirstName
    {
        get { return _firstName; }
        set
        {
            if (_firstName != value)
            {
                _firstName = value;
                OnPropertyChanged(new PropertyChangedEventArgs("FirstName"));
            }
        }
    }
}

and your XAML:
XML
<DataGrid x:Name="ContactsDataGrid" ItemsSource="{Binding}">
                                    <DataGrid.Columns>
                                        <DataGridTextColumn FontSize="14" Header="FirstName" Width="150" Binding="{Binding FirstName}"/>
                                    </DataGrid.Columns>
                                </DataGrid>

and finally in your window.xaml.cs:
ContactsDataGrid.DataContext=GetContacts();
 
Share this answer
 
The solution is not the Caliburn Micro way of doing this. If you want CM to do your binding, you need to create a BindableCollection<yourclass> SomeName and make sure that the DataGrid is SomeName in the XAML

<DataGrid x:Name="SomeName" />
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900