我正试图通过绑定检查所有复选框.. 单击按钮后,getChecked属性确实变为true,但复选框未被检查。有人看到我在这里做错了吗?这是列表框的XAML代码。
<ListBox Name="scroll" ItemContainerStyle ="{StaticResource _ListBoxItemStyle}" Tag="{Binding SortingIndex}" BorderBrush="#C62828" BorderThickness="1" Grid.Row="1" Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Name="checkboxStack">
<CheckBox IsChecked="{Binding Path=getChecked}" Content="{Binding Path=vraag}" Style="{StaticResource LifesaversCheckBoxesA}"/>
<StackPanel Margin="20,0,0,0">
<RadioButton GroupName="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListBox}, Path=Tag}" Content="{Binding Path=antwoorden[0]}" FontSize="15" />
<RadioButton GroupName="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListBox}, Path=Tag}" Content="{Binding Path=antwoorden[1]}" FontSize="15" />
<RadioButton GroupName="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListBox}, Path=Tag}" Content="{Binding Path=antwoorden[2]}" FontSize="15" />
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
这是我为vragenLijst中的每个vraag将getChecked布尔值更改为true的按钮的事件处理程序。样本数据只是生成一些随机字符串。
public partial class LivesaversWindow : UserControl
{
ObservableCollection<Vraag> vragenLijst;
public LivesaversWindow()
{
InitializeComponent();
vragenLijst = new VragenList(SampleData.vragen());
scroll.ItemsSource = vragenLijst;
}
private void alles_Selecteren(object sender, RoutedEventArgs e)
{
if ((string)select.Content == "Alles selecteren")
{
foreach(Vraag vraag in vragenLijst)
{
vraag.getChecked = true;
}
select.Content = "Alles deselecteren";
}
else
{
foreach (Vraag vraag in vragenLijst)
{
vraag.getChecked = false;
}
select.Content = "Alles selecteren";
}
}
这些是我正在使用的两个班级。
public class Vraag
{
public List<string> antwoorden { get; set; }
public string vraag { get; set; }
public Boolean getChecked { get; set; }
}
public class VragenList : ObservableCollection<Vraag>
{
public VragenList(List<Vraag> vragen) :base()
{
foreach (var vraag in vragen)
{
Add(vraag);
}
}
}
答案 0 :(得分:1)
您的班级Vraag必须实施 INotifyPropertyChanged 界面
答案 1 :(得分:1)
您的类Vraag未实现INotifyPropertyChanged接口。因此,UI不会注册对视图中显示的对象所做的任何更改:
所以你需要做的就是实现像这样的INotifyPropertyChanged-Interface:
public class Vraag : INotifyPropertyChanged
{
public List<string> antwoorden { get; set; }
public string vraag { get; set; }
private bool isChecked;
public bool getChecked
{
get { return isChecked; }
set
{
isChecked = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName] string propName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
}
}
希望这有帮助!