1
votes

I have a list of string in my viewmodel which gets populated in the run time with filenames like Events.csv etc. I am binding this list as an itemssource to the DataGrid but it shows the length of the string rather than the string. Please help.

Don't worry about the INotifyPropertyChanged and DATACONTEXT. The DataContext is set correctly to the DetailedRunInformationViewModel and other pieces of data in this view are populating correctly.

Please see the attached screenshot when I debug. Debugging List of string

ViewModel:

public class DetailedRunInformationViewModel : RunRelatedErrors
    {
public List<string> AllFilesGeneratedList { get; set; }
public DetailedRunInformationViewModel(int sessionID, RunData runData)
        {
          DisplayAllFiles();
        }
public void DisplayAllFiles()
        {
            if (_runData != null)
            {
                if (_runData.CSVDataInDataTableFormatForEachFile != null && _runData.CSVDataInDataTableFormatForEachFile.Count > 0)
                {
                    if (AllFilesGeneratedList == null)
                    {
                        AllFilesGeneratedList = new List<string>();
                    }
                    foreach(var key in _runData.CSVDataInDataTableFormatForEachFile.Keys)
                    {
                        AllFilesGeneratedList.Add(key);
                    }
                }
            }
        }
    }

In the xaml

<DataGrid
                    x:Name="AllRunFiles"
                    HorizontalAlignment="Left"
                    VerticalAlignment="Top"
                    Margin="{StaticResource AllControlsMargin}"
                    ItemsSource="{Binding AllFilesGeneratedList}"
                    AutoGenerateColumns="True">
                    <i:Interaction.Triggers>
                        <i:EventTrigger
                            EventName="SelectionChanged">
                            <i:InvokeCommandAction
                                Command="{Binding OpenSelectedFile}"
                                CommandParameter="{Binding ElementName=AllRunFiles,Path=SelectedValue}"></i:InvokeCommandAction>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                    <DataGrid.ItemTemplate>
                        <DataTemplate>
                            <TextBlock
                                Text="{Binding}"
                                Style="{StaticResource TextBlockHyperlinkStyle}"></TextBlock>
                        </DataTemplate>
                    </DataGrid.ItemTemplate>

                </DataGrid>
1
Ya i want to set the ItemTemplate because I have a hyperlink style that needs to be applied on the text. - nikhil
the CSVDataInDataTableFormatForEachFile is a Dictionary<string, DataTable>. Each string key is something like "Events.csv","A1_Rt_234.csv" etc - nikhil
I keep a breakpoint at and it clearly shows a string and not the count. - nikhil
Anyways if I use an ItemsControl instead of a DataGrid it works just fine. - nikhil

1 Answers

1
votes

Your DataGrid needs to have a Columns definitions, and make AutoGenerateColumns="False" so you have more control over how you display.

<DataGrid>
    <i:Interaction.Triggers>
    ...
    </i:Interaction.Triggers>
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="File Name">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding}"></TextBlock>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

Also I didn't check this, but I think you might need to change your TextBlock Text="{Binding}" to TextBlock Text="{Binding Path=.}".