gpt4 book ai didi

wpf - 选中的列表框 (WPF)

转载 作者:行者123 更新时间:2023-12-01 11:03:47 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com