Note: Forums will be making significant UX changes to address key usability improvements surrounding search, discoverability and navigation. To learn more about these changes please visit the announcement which can be found HERE.

Answered wpf

Toutes les réponses

  • mardi 21 août 2012 06:11
     
     
    how to use checkedlistbox control in wpf

    suresh san

  • mardi 21 août 2012 06:36
     
     

    Hi Suresh,

    I feel there are already answers to your question in the below mentioned thread :

    http://social.msdn.microsoft.com/Forums/en/wpf/thread/94c46fa1-109a-4a98-8cbd-0a5f2181fe26

  • mercredi 22 août 2012 09:48
     
     Traitée

    To be Simple copy peast the below code:

                     

    <Window.Resources>

            <DataTemplate x:Key="ListBoxDataTemplate">
                <DockPanel LastChildFill="True">
                    <CheckBox IsEnabled="False" IsChecked="{Binding IsChecked, Mode=TwoWay}" VerticalAlignment="Center" DockPanel.Dock="Left" />
                    <TextBlock Margin="10" Text="{Binding EmpName}" />
                </DockPanel>
            </DataTemplate>

        </Window.Resources>
        <Grid>
            <ListBox ItemsSource="{Binding Employees}" 
                     ItemTemplate="{StaticResource ListBoxDataTemplate}" >

                <ListBox.ItemContainerStyle>
                    <Style TargetType="{x:Type ListBoxItem}">
                        <Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}"/>
                    </Style>
                </ListBox.ItemContainerStyle>
            </ListBox>
        </Grid>

            

     public partial class Window1 : Window
        {
            List<Employee> mEmployees = new List<Employee>() { new Employee() { EmpName = "A" },
                new Employee() { EmpName = "B" }, new Employee() { EmpName = "C" }, 
                new Employee() { EmpName = "D" } };

            public Window1()
            {
                InitializeComponent();
                DataContext = this;
            }

            public List<Employee> Employees
            {
                get
                {
                    return mEmployees;
                }
            }


        }

     public class Employee : INotifyPropertyChanged
        {

            string mEmpName;
            public string EmpName
            {
                get
                {
                    return mEmpName;
                }
                set
                {
                    mEmpName = value;
                }
            }

            private bool mIsChecked;
            public bool IsChecked
            {
                get
                {
                    return mIsChecked;
                }
                set
                {
                    mIsChecked = value;
                    OnPropertyChanged("IsChecked");
                }
            }

            private bool mIsSelected;
            public bool IsSelected
            {
                get
                {
                    return mIsSelected;
                }
                set
                {
                    mIsSelected = value;
                    this.IsChecked = value;
                    OnPropertyChanged("IsSelected");
                }
            }



            #region INotifyPropertyChanged Members

            public event PropertyChangedEventHandler PropertyChanged;

            void OnPropertyChanged(string property)
            {
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs(property));
                }
            }

            #endregion
        }

    Please let me know if you have any query.

    Regards

    Ashish

    -------------------------------------------------------------------------

    Please remember to mark the replies as answered if they help.