samedi 20 février 2021

How to assign a List of Booleans to a generated Checkbox IsChecked property in WPF?

I've recently started learning C# and I've encountered a problem. I display a set of keywords in a generated checkbox in my WPF and I want to check the element (IsChecked) based on an input check from a TXT file. If the currently selected element from a different listbox matches the read modelclass(from a txt file) then set the checked key true.

I'm generating a Checkbox in my WPF to list a set of keywords that my app reads from a txt file. The txt file contains the following items per line: -id -key -pair -description

WPF code:


 <ListView ItemsSource="{Binding XAMLModelKeywords}" SelectedItem="{Binding XAMLModelKeyword}" Margin="5" x:Name="listofallkeys" Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2" >
        <ListView.ItemTemplate>
            <DataTemplate>
                <CheckBox IsChecked="{Binding XAMLAssignedKeys}" Content="{Binding Key}"/>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

C#:

        public ModelTemplates XAMLModelTemplate { get; set; }

        public ModelKeywords XAMLModelKeyword { get; set; }
        public List<bool> XAMLAssignedKeys { get; set; }


        public string XAMLKeyword { get; set; }

        public ViewModelMain()
        {
            //This creates a new instance of ObservableCollection above
            XAMLModelTemplates = new ObservableCollection<ModelTemplates>();
            XAMLModelKeywords = new ObservableCollection<ModelKeywords>();
            XAMLAssignedKeys = new List<bool>();
            Refresh();
        }

     public void Refresh()
        {
            XAMLModelTemplates.Clear();
            foreach (ModelTemplates tpl in ReadInput.ReadTemplateDirectory(Path))
            {
                XAMLModelTemplates.Add(tpl);
            }
            //Selecting the first item from the returned list
            XAMLModelTemplate = XAMLModelTemplates.FirstOrDefault();
    
            XAMLModelKeywords.Clear();
            foreach (ModelKeywords tpl in ReadInput.ReadKeywordsFile(KeyWordsPath))
            {
                XAMLModelKeywords.Add(tpl);
            }
            XAMLModelKeyword = XAMLModelKeywords.FirstOrDefault();
    
            XAMLAssignedKeys.Clear();
            foreach (ModelKeywords tpl in XAMLModelKeywords)
            {
    
                XAMLAssignedKeys.Add(ReadInput.CheckPairedtemplates(tpl, XAMLModelTemplate));
            }

ReadKeywordsFile: Returns a list of Template Models (name of template file, path) and the displays it in a listbox.

ReadKeywordsFile: Returns a list of Keywords Model (id, key, pair, desc) and then displays it in a generated listbox.

CheckPairedtemplates: Returns a list of booleans based on the currently selected Template Model matches the Keywords Model pair (list of string).

TLDR: I have a list of booleans ( XAMLAssignedKeys) and I want to match it to my generated checkbox in WPF, however the generation happens based on an item template and I'm not sure how to link one element from my list of booleans to the checkbox "IsChecked" property.

ScreenshotofApp

Thank you much in advance for the advices.




Aucun commentaire:

Enregistrer un commentaire