【问题标题】:Checkboxes in listbox not getting checked via binding未通过绑定检查列表框中的复选框
【发布时间】:2016-08-24 08:59:14
【问题描述】:

我正在尝试通过绑定检查所有复选框。 单击按钮后,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";
        }
    }

这是我正在使用的 2 个类。

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

【问题讨论】:

    标签: c# wpf checkbox binding listbox


    【解决方案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));
        }
    }
    

    希望这会有所帮助!

    【讨论】:

    • 没问题。就像一个sidenode:创建一个基类来实现带有PropertyChangedEventHandler和OnPropertyChanged-Method的INotifyPropertyChanged接口总是很不错的,所以你可以在你想在你的UI中显示的任何类中使用它。
    【解决方案2】:

    您的 Vraag 类必须实现 INotifyPropertyChanged 接口

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-03
      • 2012-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-17
      相关资源
      最近更新 更多