jeudi 18 août 2016

A Listbox of checkboxes

I found a short solution of how to implement ListBox of checkboxes here: http://ift.tt/2bhKEDq

Unfortunately I can't get it to work. I followed the steps on the website above, I also added a button, so that a list would be filled when I click it. here's my code:

    public class CsvFile
    {       
        public string Name { get; set; }
        public bool IsChecked { get; set; }
        public List<CsvLine> Lines = new List<CsvLine>();

        public CsvFile(string name)
        {
            Name = name;
        }
    }


private void Button_Click_1(object sender, RoutedEventArgs e)
{
    var files = System.IO.Directory.GetFiles(Settings.Directory, "*.csv");

    foreach (var file in files)
            {
                string fileName = System.IO.Path.GetFileNameWithoutExtension(file);
                Settings.FilesList.Add(new CsvFile(fileName));
                FilesListListBox.Items.Add(fileName);                
            }
}

public static class Settings
{
   public static List<CsvFile> FilesList = new List<CsvFile>();
   public static Directory = @"c:\";
} 

And here's XAML of my ListBox:

<ListBox x:Name="FilesListListBox" ItemsSource="{Binding Settings.FilesList}" RenderTransformOrigin="0.507,0.492" Margin="359,111,24,83" >
                <ListBox.ItemTemplate>
                    <HierarchicalDataTemplate>
                        <CheckBox Content="{Binding Name}" IsChecked="{Binding IsChecked}"/>
                    </HierarchicalDataTemplate>
                </ListBox.ItemTemplate>
</ListBox>

To be honest I have no idea how it's supposed to work, I don't have any experience with XAML and WPF, so I'm not able to find out what's wrong, although I think I've got something wrong with the part where i add items to the ListBox.

I hope someone can help me out.

Aucun commentaire:

Enregistrer un commentaire