【问题标题】:How to assign a List of Booleans to a generated Checkbox IsChecked property in WPF?如何将布尔值列表分配给 WPF 中生成的 Checkbox IsChecked 属性?
【发布时间】:2021-05-23 07:52:37
【问题描述】:

我最近开始学习 C#,但遇到了一个问题。我在 WPF 中生成的复选框中显示一组关键字,我想根据来自 TXT 文件的输入检查来检查元素 (IsChecked)。 如果当前从不同列表框中选择的元素与读取的模型类(来自 txt 文件)匹配,则将选中的键设置为 true。

我正在 WPF 中生成一个复选框,以列出我的应用从 txt 文件中读取的一组关键字。 txt 文件每行包含以下项目: -ID -钥匙 -一对 -描述

WPF 代码:


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

型号关键字:

    public class ModelKeywords
    {
        public int Id { get; set; }
        public string Key { get; set; }
        public List<string> PairedTemplates { get; set; }
        public string Description { get; set; }
    }

模型模板:

    public class ModelTemplates
    {
        //path to a template
        public int Id { get; set; }
        public string TemplatePath { get; set; }
        public string TemplateName { get; set; }
        public ExcelRange TemplateRange { get; set; }
    }

读取关键字文件: 返回模板模型列表(模板文件名、路径)并将其显示在列表框中。

读取关键字文件: 返回关键字模型列表(id、key、pair、desc),然后将其显示在生成的列表框中。

检查配对模板: 根据当前选择的模板模型匹配关键字模型对(字符串列表)返回布尔列表。

TLDR: 我有一个布尔值列表( XAMLAssignedKeys ),我想将它与 WPF 中生成的复选框相匹配,但是生成是基于项目模板发生的,我不确定如何将布尔值列表中的一个元素链接到复选框“IsChecked”属性。

ScreenshotofApp

非常感谢您的建议。

【问题讨论】:

  • 您的代码不完整。 ModelKeywords的内容是什么?
  • 您好!这是一个简单的模型类。以下是内容:` public class ModelKeywords { public int Id { get;放; } 公共字符串密钥 { 获取;放; } public List PairedTemplates { get;放; } 公共字符串描述 { 获取;放; } } `
  • 我建议您自己完善您的问题,添加该类。此外,您的类实现了 INotifyPropertyChanged?它是 WPF MVVM 模式的基础。
  • 在我的问题中添加了模型。我会调查这个 INotifyPropertyChanged。

标签: c# wpf checkbox


【解决方案1】:

由于您将 ObservableCollection (XAMLModelKeywords) 设置为 ListView (listofallkeys) 的 ItemsSource 属性,因此 ListView 的每个项目都将绑定到 ObservableCollection 的成员。在这种情况下,DataTemplate 中的 CheckBox 将绑定到 XAMLModelKeywords 中的 ModelKeywords。因此,需要将 CheckBox 的属性与 ModelKeywords 的属性进行绑定。

在 Xaml 中,您错误地将 XAMLAssignedKeys 设置为 CheckBox 的 IsChecked 属性。它必须是 ModelKeywords 的属性。补救方法是向 ModelKeywords 添加一个 bool 属性,以某种方式将 XAMLAssignedKeys 中的一个值复制到该属性,然后将该属性与 CheckBox 的IsChecked 属性绑定。

假设,如果将 IsEnabled bool 属性添加到 ModelKeywords,Xaml 将是

<CheckBox IsChecked="{Binding IsEnabled}" Content="{Binding Key}"/>

另外,除非你对ModelKeywords实现INotifyPropertyChanged接口,否则只有属性的初始值会被发送到CheckBox,而后续的值不会在属性改变时被通知并反映到CheckBox。

【讨论】:

猜你喜欢
  • 2010-11-05
  • 2012-11-15
  • 1970-01-01
  • 2011-01-25
  • 1970-01-01
  • 2016-04-26
  • 2016-01-13
  • 1970-01-01
相关资源
最近更新 更多