简体   繁体   English

Datagrid ItemsSource将不会添加列表

[英]Datagrid ItemsSource won't add the List

Good Morning, I'm facing a very strange problem with a DataGrid in WPF. 早上好,我在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. 我也尝试过(在foreach中)打印出file.Name,并且得到了正确的输出。 I don't really know why is not working. 我真的不知道为什么不起作用。 The only thing that I get every time is the file.Name.Length value. 我每次得到的唯一东西是file.Name.Length值。

LoadRecipe

Somebody can give me an hint? 有人可以给我提示吗?

Thank you in advance 先感谢您

EDIT 编辑

Here's the XAML: 这是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>

in How come my databinding is writing out the Length property? 我的数据绑定如何写出Length属性? 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 . 我回答了Length列的来源:它是由DataGrid自动生成的,因为默认情况下启用了属性的列自动生成功能,并且string仅具有Length属性。

to get rid of "Length" column, set AutoGenerateColumns="False" and define <DataGrid.Columns> like in the linked question. 要摆脱“长度”列,请设置AutoGenerateColumns="False"并像链接的问题一样定义<DataGrid.Columns>

Binding string collection to DataGrid is a known "gotcha": WPF: Bind DataGrid to List<String> . 将字符串集合绑定到DataGrid是已知的“陷阱”: WPF:将DataGrid绑定到List <String> DataGrid is supposed to be editable, but editing won't work with string items. DataGrid应该是可编辑的,但是编辑不适用于字符串项。


For your situation - display a list of string with possibility to select them - it is simpler to use ListBox instead of DataGrid 根据您的情况-显示字符串列表并可以选择它们-使用ListBox代替DataGrid更简单

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM