0
votes

I wrote the below code in wpf datagrid

<DataGridTextColumn Binding="{Binding comments}" Width="350" Header="Comments"  IsReadOnly="False" >
    <DataGridTextColumn.ElementStyle>
        <Style>
            <Setter Property="TextBlock.TextWrapping" Value="Wrap" />
            <Setter Property="TextBlock.TextAlignment" Value="Left"/>
        </Style>
    </DataGridTextColumn.ElementStyle>
</DataGridTextColumn>

And getting the error below, but the grid is working fine. Can anyone please help me to identify why I am getting the below error.

System.Windows.Data Error: 40 : BindingExpression path error: 'comments' property not found on 'object' ''DataRowView' (HashCode=43816328)'. BindingExpression:Path=comments; DataItem='DataRowView' (HashCode=43816328); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

1
You are using an incorrect binding for the column. I suggest you to post all the code of the datagrid.Babbillumpa
Thank you Babbillumpa.Your comments and the sample code posted by Peregrine helped a lot. I misspelled the column name Comments in the binding expression. Instead of uppercase "C", I have a lower case "c" and this caused the binding expression error.ank-2007

1 Answers

0
votes

Please post a Minimal, Complete, and Verifiable example that reproduces your issue.

The code below works fine for me.

DataItem.cs

public class DataItem
{
    public string A => "AAAAAAAAAA AAAAAAAAAA";

    public string B => "BBBBBBBBBB";

    public string C => "CCCCCCCCCC";
}

MainViewModel.cs

public class MainViewModel
{
    public MainViewModel()
    {
        DataItems = new List<DataItem>();

        for (var i = 1; i <= 20; i++)
            _dataItemList.Add(new DataItem());
    }

    public List<DataItem> DataItems {get;}
}

MainView.xaml

<Window ...>
    <Window.DataContext>
        <local:MainViewModel/>
    </Window.DataContext>

    <DataGrid Margin="16" ItemsSource="{Binding DataItems}" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="A" Binding="{Binding A}" Width="100">
                <DataGridTextColumn.ElementStyle>
                     <Style>
                        <Setter Property="TextBlock.TextWrapping" Value="Wrap" />
                        <Setter Property="TextBlock.TextAlignment" Value="Left"/>
                     </Style>
                </DataGridTextColumn.ElementStyle>
            </DataGridTextColumn>

            <DataGridTextColumn Header="B" Binding="{Binding B}"/>

            <DataGridTextColumn Header="C" Binding="{Binding C}"/>
        </DataGrid.Columns>
    </DataGrid>
</Window>