Bind the object to ListBox. The Items status is inconsistent with expectation.

Source: Internet
Author: User

I recently read discussion. I have a very interesting topic:
Defines a class

Class DisplayedItem {public DisplayedItem (string name, bool isChecked) {Name = name; IsChecked = isChecked;} public string Name {get; set;} public bool IsChecked {get; set ;}} this class has an IsChecked bool type. To bind this object set to ListBox, each ListBoxItem is displayed as CheckBox.
    ObservableCollection<DisplayedItem> col = new ObservableCollection<DisplayedItem>();    private void Window_Loaded(object sender, RoutedEventArgs e)    {        col.Add(new DisplayedItem("item1", false));        col.Add(new DisplayedItem("item2", false));
        col.Add(new DisplayedItem("item3", false));
        col.Add(new DisplayedItem("item4", false));
        col.Add(new DisplayedItem("item5", false));
        col.Add(new DisplayedItem("item6", false));
        col.Add(new DisplayedItem("item7", false));
        col.Add(new DisplayedItem("item8", false));        col.Add(new DisplayedItem("item9", false));        col.Add(new DisplayedItem("item10", false));        col.Add(new DisplayedItem("item11", false));        col.Add(new DisplayedItem("item12", false));        col.Add(new DisplayedItem("item13", false));        col.Add(new DisplayedItem("item14", false));        col.Add(new DisplayedItem("item15", false));        col.Add(new DisplayedItem("item16", false));         listBox1.ItemsSource = col;    }
<ListBox Name="listBox1" Margin="198,12,0,48" HorizontalAlignment="Left" Width="146" Height="120">        <ListBox.ItemTemplate>           <DataTemplate>               <CheckBox Content="{Binding Path=Name}"                          IsChecked="{Binding Path=IsChecked, Mode=TwoWay}"/>           </DataTemplate>        </ListBox.ItemTemplate></ListBox> 

There is a Button on the interface. When you click it, all the checkboxes are checked.

private void button1_Click(object sender, RoutedEventArgs e){    foreach (var item in col)    {       item.IsChecked = true;    }}

The actual result after running is strange.

Initial:

Figure 1

After clicking the Button, pull the scroll bar to the bottom and you will find that item10 and item16 are hooked up, but item1 and item9 are not hooked up.

Figure 2

During debugging, we can find that the IsChecked of the actually bound object is true.

Study the code and click "Button" to check all items. This is a good implementation, as long as DisplayedItem is implemented.INotifyPropertyChanged. For details, see bind to. net attributes/objects.

 

But I can't help wondering why not.INotifyPropertyChangedIn this case, why are some of them hooked up, some are not hooked up, And the strange thing is, before clicking the Button, pull the scroll bar to the bottom, and then click the Button, you will find that all items are not hooked up:

Figure 3

How does this weird and incredible thing happen? Does WPF have a bug?

Fortunately, some people reply to this question. My understanding is as follows:

Because it is not implementedINotifyPropertyChangedThe OneTime mode is used for binding to ListBox. In ListBox, some items (Item1 to Item7) are displayed in the initial state, and some (Item8 to Item16) are not displayed. If the displayed value is false when OneTime is used for display, the undisplayed value is not bound for performance consideration. When you click the Button, the source is already true. When you pull the scroll bar to the bottom, there is a display process, and the result is calculated as true, so the UI will be checked. If the scroll bar is pulled to the bottom, all items are calculated by OneTime, and the value is false. click the button. Although the source has been set to true, it is not implemented.INotifyPropertyChangedTherefore, source changes are not displayed on the UI.

This is indeed the case. If OneTime is used, you can set the initial status of Item6 and Item7 to true, which can be observed.

Figure 4 after the ListBox height is increased, the initial values of Item6 and Item7 are set to true.

However, the students carefully discovered that Item8 and Item9 are not displayed in the initial situation. Why did they still fail to be checked after clicking the button?

This is really strange, and someone else is passing:
To make the display smooth, WPF also binds some undisplayed items (close to the displayed ones). Here, Item8 and Item9 are used.

I did an experiment and increased the height of ListBox. I did not check the previous ones:

Figure 5 after the height is increased, Item12 and Item13 are not checked

 

 

There is only one truth.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.