【发布时间】:2011-12-29 13:42:07
【问题描述】:
我无法让已选中的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;
其中_shops 是ObservableCollection<shop>(包含两个元素)。
我得到的是列表框中的两个 空白 复选框(没有标题,并且都勾选了,即使 selected 为 ItemsSource 中的所有项目设置为 true)。
我已经很沮丧了,我敢肯定这一定是一件非常微不足道的事情。这里有什么问题?
【问题讨论】:
-
你只能绑定到公共属性,所以如果你的类是嵌套的和私有的,我认为这不会起作用。您可以查看 Visual Studio 中的“输出”窗口以了解绑定失败的位置。
标签: wpf data-binding checkbox listbox c#-3.0