Reputation: 152
Good Morning, I'm facing a very strange problem with a DataGrid in WPF. I'm trying to add the list of file inside a specific folder, but the grid remains blank. Could you please explain me why? That's my code:
private static readonly string _sharedFolder = Settings.GetShared();
private readonly DirectoryInfo _disF = new DirectoryInfo(_sharedFolder);
private void LoadRecipe_OnLoaded(object sender, RoutedEventArgs e)
{
FileInfo[] _sFFiles = _sF.GetFiles("*.csv");
List<string> filesList = new List<string>();
foreach (FileInfo file in _sFFiles)
{
filesList.Add(file.Name);
}
Resources.MergedDictionaries.Add(Function.SetLanguageDictionary());
Title = Function.GetTranslatedValue("LoadRecipe", Settings.GetLang());
DatagridRecipes.ItemsSource = filesList;
FoundRecipesLabel.Content = Function.GetTranslatedValue("FoundRecipes", Settings.GetLang());
ButtonLoadRecipe.Content = Function.GetTranslatedValue("Load", Settings.GetLang());
}
I've also tried (inside the foreach) to print out the file.Name and I've got the right output.
I don't really know why is not working.
The only thing that I get every time is the file.Name.Length
value.
Somebody can give me an hint?
Thank you in advance
EDIT
Here's the XAML:
<Grid>
<Button x:Name="ButtonLoadRecipe" Content="Load" HorizontalAlignment="Stretch" Margin="10,0,10,10" VerticalAlignment="Bottom" Style="{DynamicResource SquareMetroButton}" Height="40" Click="ButtonLoadRecipe_Click"/>
<DataGrid x:Name="DatagridRecipes" HorizontalAlignment="Left" Height="351" Margin="10,65,0,0" VerticalAlignment="Top" Width="274" AutoGenerateColumns="False" SelectionChanged="DatagridRecipes_SelectionChanged"/>
<Label x:Name="FoundRecipesLabel" Content="RecipesFound" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" FontSize="16" FontWeight="Bold"/>
<Separator HorizontalAlignment="Left" Height="19" Margin="10,46,0,0" VerticalAlignment="Top" Width="274"/>
</Grid>
Upvotes: 0
Views: 97
Reputation: 35646
in How come my databinding is writing out the Length property? I answered where Length
column comes from: it is auto-generated by DataGrid because columns auto-generation for properties is enabled by default and string has only property Length
.
to get rid of "Length" column, set AutoGenerateColumns="False"
and define <DataGrid.Columns>
like in the linked question.
Binding string collection to DataGrid is a known "gotcha": WPF: Bind DataGrid to List<String>. DataGrid is supposed to be editable, but editing won't work with string items.
For your situation - display a list of string with possibility to select them - it is simpler to use ListBox
instead of DataGrid
Upvotes: 1