wpf - 选中的列表框 (WPF)

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

我无法检查 ListBox去工作。

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

    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>

后台代码中的数据绑定(bind)非常简单:

lbSelectedShops.ItemsSource = _shops;

哪里_shops是一个 ObservableCollection<shop> (包含两个元素)。

我得到的是列表框中的两个空白复选框(没有标题,并且都勾选了,即使 selected 设置为 true 对于 ItemsSource 中的所有项目)。

我已经很沮丧了,我敢肯定这一定是一件非常微不足道的事情。这里有什么问题?

最佳答案

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

You can bind to public properties, sub-properties, as well as indexers, of any common language runtime (CLR) object. The binding engine uses CLR reflection to get the values of the properties. Alternatively, objects that implement ICustomTypeDescriptor or have a registered TypeDescriptionProvider also work with the binding engine.

关于wpf - 选中的列表框 (WPF),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8137592/

相关文章:

wpf - 如何刷新 WPF DataGrid?

c# - 在 GridView 中引用单元格中的文本框

wpf - 为什么我的带有 DrawingBrush 填充绑定(bind)到前景色的 ToggleButton 不起作用? VS2012

我的待办事项列表上的复选框不起作用

jQuery - 快速点击后标签/输入中断的切换类

jquery - 如何更改/添加 css 到动态生成的复选框类

c# - WPF自定义控件设计错误

c# - WPF如何更改首先打开的窗口

c# - WPF 绑定(bind) DataGrid - 我可以添加一个未绑定(bind)的列来显示状态吗?

wpf - 如何使用 WPF 和 MVVM 将数据库中的数据加载到 DataGrid?