![](/img/trans.png)
[英]In WPF, how do I two-way bind a Checkbox's IsChecked to property to List<>.Contains?
[英]How to assign a List of Booleans to a generated Checkbox IsChecked property in WPF?
最近開始學習C#,遇到了一個問題。 我在 WPF 的生成復選框中顯示一組關鍵字,我想根據來自 TXT 文件的輸入檢查來檢查元素 (IsChecked)。 如果當前從不同列表框中選擇的元素與讀取的模型類(來自 txt 文件)匹配,則將選中的鍵設置為 true。
我在 WPF 中生成一個復選框,以列出我的應用從 txt 文件中讀取的一組關鍵字。 txt 文件每行包含以下項目: -id -key -pair -description
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; }
}
ReadKeywordsFile:返回模板模型列表(模板文件名、路徑)並將其顯示在列表框中。
ReadKeywordsFile:返回關鍵字 Model(id、key、pair、desc)的列表,然后將其顯示在生成的列表框中。
CheckPairedtemplates:根據當前選擇的模板 Model 匹配關鍵字 Model 對(字符串列表)返回布爾列表。
TLDR:我有一個布爾值列表(XAMLAssignedKeys),我想將它與我在 WPF 中生成的復選框相匹配,但是生成是基於項目模板發生的,我不確定如何將我的布爾值列表中的一個元素鏈接到復選框“IsChecked”屬性。
非常感謝您的建議。
由於您將 ObservableCollection<ModelKeywords> (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。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.