【问题标题】:WPF: How to Statically Initialize a List of CheckBoxes?WPF:如何静态初始化复选框列表?
【发布时间】:2014-08-19 17:21:06
【问题描述】:

如何静态初始化复选框列表?我无法完全正确地设置初始化程序。

【问题讨论】:

    标签: wpf list checkbox listbox observablecollection


    【解决方案1】:

    首先,在 XAML 中,创建一个包含 CheckBox 的 ListBox。绑定所需的元素。

    <ListBox x:Name="Pets" >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <CheckBox Content="{Binding Content}" IsChecked="{Binding IsChecked}" Click="Pets_Click"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    

    最后,使用初始化器创建一个 CheckBox 列表。

    Pets.ItemsSource = new System.Collections.ObjectModel.ObservableCollection<CheckBox>()
        {
            { new CheckBox() {Content = "Cat", IsChecked = true} },
            { new CheckBox() {Content = "Dog", IsChecked = true} },
            { new CheckBox() {Content = "Goldfish", IsChecked = false} },
        };
    

    【讨论】:

      猜你喜欢
      • 2011-09-08
      • 2013-04-29
      • 2016-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-17
      相关资源
      最近更新 更多