Programmatically selecting Items/Indexes in a ListBox

前端 未结 6 1897
情深已故
情深已故 2021-01-07 23:24

In WPF, I\'d like to set the selected indexes of a System.Windows.Controls.ListBox

I best way I\'ve found so far is to remove all the items from the control, insert

6条回答
  •  一整个雨季
    2021-01-07 23:51

    Thanks to mdm20. My case was actually checking a CheckBox within the ListBox, and this Dependency Property worked like a charm. I had to inherit my custom class from DependencyObject and implement the property

    public class ProjectListItem : DependencyObject{ 
    
        public Boolean IsChecked
        {
            get { return (Boolean)this.GetValue(CheckedProperty); }
            set { this.SetValue(CheckedProperty, value); }
        }
        public static readonly DependencyProperty CheckedProperty =
            DependencyProperty.Register("IsChecked", typeof(Boolean), typeof(ProjectListItem), 
                                        new PropertyMetadata(false));
    }
    

提交回复
热议问题