Search code examples
wpfxamlbindingdatagrid

Why I can't see the value of binding in my datagrid


I try to use with bindinig in Datagrid but it is not show me the values

that is my code :

in the xaml:

<Grid DataContext="ListFromFolderToFileTransferViewModel">
    <DataGrid Name="datagrid" SelectionChanged="selectionChanged" ItemsSource="{Binding Source=Folders,UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" CanUserAddRows="False"   >
        <DataGrid.Columns>
            <DataGridTextColumn  Header="Folders" Binding="{Binding Source=Folders}" Foreground="Black" IsReadOnly="false" />
        </DataGrid.Columns>
    </DataGrid>
</Grid>

in the xaml.cs :

public ListFromFolderToFileTransferView()
    {
        InitializeComponent();
        DataContext = new ListFromFolderToFileTransferViewModel();
    }

in the viewModel:

    object _lock = new object();
    public event PropertyChangedEventHandler PropertyChanged;
    ObservableCollection<string> _Folders=new ObservableCollection<string>();;
    public ObservableCollection<string> Folders { get => _Folders; set { _Folders = value; OnPropertyChanged("Folders"); } }

    public ListFromFolderToFileTransferViewModel()
    {
        BindingOperations.EnableCollectionSynchronization(Folders, _lock);                   
        string[] folders = Directory.GetDirectories(FilePath);
        foreach (var folder in folders)
        {
            DirectoryInfo d = new DirectoryInfo(folder);
            Folders.Add(d.Name);
        }
    }

and I see table taht in evry line its write for me Folders and dont show the values

someone can help me thank you very much


Solution

  • The expression

    <Grid DataContext="ListFromFolderToFileTransferViewModel">
    

    assigns the string "ListFromFolderToFileTransferViewModel" to the DataContext of the Grid, which is then inherited by the DataGrid. That string does of course not have a Folders property.

    The binding expression

    ItemsSource="{Binding Source=Folders,UpdateSourceTrigger=PropertyChanged}"
    

    is wrong. You have to set the Binding's Path, not its Source, and setting its UpdateSourceTrigger is pointless because it is a OneWay Binding.

    The expression

    Binding="{Binding Source=Folders}"
    

    on the DataGridTextColumn is also wrong, because a string item in the Folders collection does not have a Folders property, and again, Source is wrong.

    Remove the DataContext assignment from the Grid, and use correct Binding expressions:

    <Grid>
        <DataGrid ItemsSource="{Binding Path=Folders}" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Folders" Binding="{Binding Path=.}"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
    

    or - because you can omit Path in a Binding expression:

    <Grid>
        <DataGrid ItemsSource="{Binding Folders}" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Folders" Binding="{Binding}"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>