我使用 this 实现了一个 ListBox
和 this .我将我的 29 个对象的实际列表绑定(bind)到它并且它运行良好。
在 XAML 中:
<ListBox Name="WBauNrList" ItemsSource="{Binding}" Grid.Row="7" Grid.Column="2" ScrollViewer.VerticalScrollBarVisibility="Visible" ScrollViewer.CanContentScroll="True" Height="100" >
<ListBox.ItemTemplate>
<HierarchicalDataTemplate>
<CheckBox Content="{Binding Baunr}" IsChecked="{Binding IsChecked,Mode=TwoWay}"/>
</HierarchicalDataTemplate>
</ListBox.ItemTemplate>
</ListBox>
在代码中:
datenpunktList = new ObservableCollection<Datenpunkt>();
foreach (var d in WerkstattList.DistinctBy(p => p.lokNr))
{
var newd = new Datenpunkt() { Baunr = d.lokNr };
datenpunktList.Add(newd);
}
WBauNrList.ItemsSource = datenpunktList;
但是问题:
我想要一个全选 CheckBoxes
以使用户能够选择和取消选择所有项目。它的工作方式很奇怪!
选中selectAll CheckBox
后,将选中所有不在滚动条范围内的项目(列表滚动),然后我应该向下和向上滚动以查看所有项目检查。
XAML:
<CheckBox Name="selectAll" Click="selectAll_Click" >Secelct all</CheckBox>
代码:
private void selectAll_Click(object sender, RoutedEventArgs e)
{
foreach (Datenpunkt item in WBauNrList.Items)
{
item.IsChecked = true ;
}
}
我不知道该怎么做。
提前致谢,莫
最佳答案
您的属性 IsChecked
实现可能如下所示。
public class Datenpunkt : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void Notify(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private bool _isChecked;
public bool IsChecked
{
get { return _isChecked; }
set
{
_isChecked = value;
Notify("IsChecked");
}
}
}
看看 MSDN INotifyPropertyChanged页面了解更多信息。
关于c# - 选中 WPF 中的全选复选框不会更改列表框中的其他复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45035503/