1

I want bind property of model to datagrid, i can't do it I have property in model model contains list with list of strings rows count is constant in list

public class ViewModelBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public class TestViewModel : ViewModelBase
    {
        public TestViewModel()
            : this(new FileService())
        {
        }

        public TestViewModel(IFileService fileService)
        {

            var list = new List<List<string>>();
            list.Add(new List<string>() { "1", "2", "3" });
            list.Add(new List<string>() { "3", "4", "5" });
            RecodListFromCsv = list;
        }

        private List<List<string>> _RecodListFromCsv;

        public List<List<string>> RecodListFromCsv
        {
            get { return _RecodListFromCsv; }
            set
            {
                if (_RecodListFromCsv != value)
                {
                    _RecodListFromCsv = value;
                    OnPropertyChanged("RecodListFromCsv");
                }
            }
        }
    }

xaml

<Window x:Class="Test.Views.TestView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:viewModel="clr-namespace:Test.ViewsModel"
        Title="PriceList" Height="427" Width="746">
    <Window.Resources>
        <viewModel:TestViewModelx:Key="TM" />
    </Window.Resources>    
    <DockPanel LastChildFill="True" DataContext="{Binding Source={StaticResource TM}}">
            <DataGrid AutoGenerateColumns="True" ItemsSource="{Binding RecodListFromCsv}">
            </DataGrid>
        </Menu>
    </DockPanel>
</Window>

and UI show enter image description here

What I'm doing wrong. how to do it?

1 Answer 1

1

Each row item of datagrid is type of List<string> so auto generating column of datagrid generate public property of List<string> object(Capacity,Count)!. So to resolve this quickly instead of List<List<string>> using List<Tuple<string,string,string>> if your items number is 3.

var list = new List<Tuple<string,string,string>>();
list.Add(new Tuple<string,string,string>() { "1", "2", "3" });
list.Add(new Tuple<string,string,string>() { "3", "4", "5" });
RecodListFromCsv = list;

The stable solution is create a persistent class and create list of that object class properly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.