[英]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
值。
有人可以給我提示嗎?
先感謝您
編輯
這是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.