繁体   English   中英

Datagrid ItemsSource将不会添加列表

[英]Datagrid ItemsSource won't add the List

早上好,我在WPF中遇到了一个非常奇怪的问题。 我正在尝试在特定文件夹内添加文件列表,但是网格仍然为空白。 你能解释一下为什么吗? 那是我的代码:

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());
}

我也尝试过(在foreach中)打印出file.Name,并且得到了正确的输出。 我真的不知道为什么不起作用。 我每次得到的唯一东西是file.Name.Length值。

LoadRecipe

有人可以给我提示吗?

先感谢您

编辑

这是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>

我的数据绑定如何写出Length属性? 我回答了Length列的来源:它是由DataGrid自动生成的,因为默认情况下启用了属性的列自动生成功能,并且string仅具有Length属性。

要摆脱“长度”列,请设置AutoGenerateColumns="False"并像链接的问题一样定义<DataGrid.Columns>

将字符串集合绑定到DataGrid是已知的“陷阱”: WPF:将DataGrid绑定到List <String> DataGrid应该是可编辑的,但是编辑不适用于字符串项。


根据您的情况-显示字符串列表并可以选择它们-使用ListBox代替DataGrid更简单

暂无
暂无

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

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