Checked ListBox(WPF)

时间:2011-11-15 14:08:56

标签: wpf data-binding checkbox listbox c#-3.0

我无法检查ListBox是否正常工作。

我的业务对象(它是私有/嵌套类,因此是小写的)

    class shop : System.ComponentModel.INotifyPropertyChanged
    {
        internal int id;
        string _name;
        internal string name
        {
            get { return _name; }
            set
            {
                _name = value;
                if (PropertyChanged != null) PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs("name"));
            }
        }

        bool _selected;
        internal bool selected
        {
            get { return _selected; }
            set
            {
                _selected = value;
                if (PropertyChanged != null) PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs("selected"));
            }
        }
    }

我的XAML:

<ListBox ItemsSource="{Binding}" Grid.Row="1" HorizontalAlignment="Stretch" Margin="10,0,10,0" Name="lbSelectedShops" VerticalAlignment="Stretch">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <CheckBox Width="Auto" Content="{Binding Path=name}" IsChecked="{Binding Path=selected, Mode=TwoWay}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>                                
</ListBox>

代码隐藏中的数据绑定非常简单:

lbSelectedShops.ItemsSource = _shops;

其中_shopsObservableCollection<shop>(包含两个元素)。

我得到的是列表框中的两个空白复选框(没有标题,并且两者都勾选了,即使selectedtrue设置为ItemsSource {1}})。

我已经非常沮丧了,我确信它一定是非常微不足道的。这有什么不对?

2 个答案:

答案 0 :(得分:3)

它不起作用,因为您的属性是内部的,对于数据绑定,您需要公共属性 来自MSDN(Binding Sources Overview):

  

您可以绑定到公共属性,子属性以及   任何公共语言运行时(CLR)对象的索引器。绑定   引擎使用CLR反射来获取属性的值。   或者,实现ICustomTypeDescriptor或具有的对象   注册的TypeDescriptionProvider也可以使用绑定引擎。

答案 1 :(得分:3)

仅与公共属性(和公共类)绑定工作

相关问题