我正在定義一個復選框串列,如下所示:
<ListBox Name="listBoxZone" ItemsSource="{Binding Nr5GRRCList}" Background="Azure" Margin="346,93,89,492" Grid.Column="1">
<ListBox.ItemTemplate>
<DataTemplate>
<ListBoxItem IsSelected="{Binding IsChecked}">
<CheckBox x:Name="RRC5G_CheckBox"
Content="{Binding messageType}"
IsChecked="{Binding IsChecked}"
Checked="RRC5G_CheckBox_Checked"
Unchecked="RRC5G_CheckBox_Unchecked"
Margin="0,5,0,0"/>
</ListBoxItem>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
其中 Nr5GRRCList 相關代碼為:
public ObservableCollection<BoolStringClass> Nr5GRRCList { get; set; }
public class BoolStringClass
{
public string messageType { get; set; }
public bool IsChecked { get; set; }
}
if (Nr5GRRCList == null)
{
Nr5GRRCList = new ObservableCollection<BoolStringClass>();
}
foreach(string filter in rrc5GFilters)
{
Nr5GRRCList.Add(new BoolStringClass { messageType = filter, IsChecked = false });
}
這作業正常:
我正在嘗試添加一個復選框來控制此串列中的所有復選框:
- 我仍然可以單獨選中/取消選中復選框
- 我希望能夠選中/取消選中新復選框并選中/取消選中所有復選框
我嘗試添加新的復選框:
<CheckBox x:Name="checkBox_NR5G_RRC" Content="RRC" Checked="HandleCheck_RRC5G" Unchecked="HandleUncheck_RRC5G" Height="Auto" Width="Auto" Margin="334,76,341,607" Grid.Column="1"/>
當檢查/未選中“ RRC”時,我找不到修改每個復選框的簽名值的方法。我似乎只能訪問 BoolStringClass 元素的串列。
任何提示將不勝感激。謝謝!
uj5u.com熱心網友回復:
我似乎只能訪問 BoolStringClass 元素的串列
這就是您需要的所有資料。
foreach(BoolStringClass item in Nr5GRRCList)
{
item.IsChecked = !item.IsChecked;
// item.IsChecked = true;
// item.IsChecked = false;
}
要在 UI 中查看更新,您需要在 BoolStringClass 中添加通知,例如通過實作 INotifyPropertyChanged:
public class BoolStringClass: INotifyPropertyChanged
{
public string messageType { get; set; }
private bool _IsChecked;
public bool IsChecked
{
get { return _IsChecked; }
set
{
_IsChecked = value;
OnPropertyChanged(nameof(IsChecked));
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/489989.html