Trying to create the first WPF application using MVVM. I try to display the list in the DataGrid, it seems to see the list, but displays the length of the string instead of text. Here is my small window

<Window x:Class="SetAbsElevation.View.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:SetAbsElevation.ViewModel" mc:Ignorable="d" Title="{Binding Title}" Height="500" Width="500"> <Window.DataContext> <local:MainWindowViewModel /> </Window.DataContext> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"></ColumnDefinition> <ColumnDefinition Width="200"> </ColumnDefinition> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition></RowDefinition> <RowDefinition Height="80"></RowDefinition> </Grid.RowDefinitions> <DataGrid Grid.Column="0" Grid.Row="0" Grid.RowSpan="2" ItemsSource="{Binding ListParameterCollection}"> </DataGrid> <Button Name="Button" Grid.Column="1" Grid.Row="1" Margin="10,10,10,10" Content="Выполнить" FontSize="16" Command="{Binding CommandButton}"/> </Grid> 

The collection that is passed to the DataGrid

 vm.ListParameterCollection = new ObservableCollection<string> { "dddddd", "lggds" }; ; 

At the output, it shows me the length of the lines, but I want to see the lines themselves "dddddd", "lggds" . enter image description here

How can I fix this?

    1 answer 1

    Here is an option without additional classes

     <DataGrid Grid.Column="0" Grid.Row="0" Grid.RowSpan="2" ItemsSource="{Binding ListParameterCollection}"> <DataGrid.Columns> <DataGridTextColumn Binding="{Binding}" Foreground="Black" Width="60" Header="Test" /> </DataGrid.Columns> </DataGrid> 

    OR

    Define class

     public class MyClass { public string Data { get; set; } } 

    And bind the collection of this class to the grid.

     vm.ListParameterCollection = new ObservableCollection<MyClass> { new MyClass() {Data = "ddddd"}}; ; 
    • In the first option, you probably won't be able to edit the lines ... - Andrey NOP
    • @AndreyNOP yes I just copied from the answer to EnSO, removed the ReadOnly flag - tym32167 February
    • Yes, no, it’s most likely that the ReadOnly flag will not be removed, although I didn’t check it, I just intuitively feel, but we don’t know if it really needs a vehicle - Andrey NOP
    • @AndreyNOP let's hope that TSA is already doing well :) - tym32167 February