我无法检查 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/