3

I have a ListBox contains checkboxes in DataTemplate and I have two buttons "Select All" and "UnSelect All".I want to make that checkboxes to check all and uncheck all with clicking select and unselect buttons and I want to implement INotifyPropertyChanged to class. How Can I do that things?

Thanks for your answers in advance..

XAML CODE

  <StackPanel>
        <ListBox Name="lstUserDefination" 
 ScrollViewer.VerticalScrollBarVisibility="Auto" SelectionMode="Multiple">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <ListBoxItem>
                    <CheckBox Name="chkUser" Content="{Binding AuthorityName}"/>
                    </ListBoxItem>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </StackPanel>

C# CODE

   public partial class UserDefinationEdit : Window
{
    public ObservableCollection<Authority> authorityList { get; set; }

    public UserDefinationEdit()
    {
        InitializeComponent();
        CreateCheckBoxList();
        lstUserDefination.ItemsSource = authorityList;
    }


    private void Window_MouseDown(object sender, MouseButtonEventArgs e)
    {
        if (e.LeftButton == MouseButtonState.Pressed)
        {
            DragMove();
        }
    }

    public void CreateCheckBoxList()
    {
        authorityList = new ObservableCollection<Authority>();

        authorityList.Add(new Authority() {AuthorityValue = 0, AuthorityName = "0 - " });
        authorityList.Add(new Authority() { AuthorityValue = 1, AuthorityName = "1 - " });
        authorityList.Add(new Authority() { AuthorityValue = 2, AuthorityName = "2 - " });
        authorityList.Add(new Authority() { AuthorityValue = 3, AuthorityName = "3 - " });
        authorityList.Add(new Authority() { AuthorityValue = 4, AuthorityName = "4 - " });
        authorityList.Add(new Authority() { AuthorityValue = 5, AuthorityName = "5 - " });
        authorityList.Add(new Authority() { AuthorityValue = 6, AuthorityName = "6 - " });


        this.DataContext = this;
    }

    private void btnUnselectall_Click(object sender, RoutedEventArgs e)
    {
        lstUserDefination.UnselectAll();
    }

    private void btnSelectAll_Click(object sender, RoutedEventArgs e)
    {
        lstUserDefination.SelectAll();
    }

}
public class Authority
{
    public string AuthorityName { get; set; }
    public int AuthorityValue { get; set; }
    public bool IsChecked { get; set; }
}
}

2 Answers 2

5

Add binding for IsChecked property in ListBoxItem template

<CheckBox Name="chkUser" Content="{Binding AuthorityName}" IsChecked="{Binding IsChecked}"/>

And change your button handlers to

private void btnUnselectall_Click(object sender, RoutedEventArgs e)
{
    foreach (var a in authorityList)
    {
        a.IsChecked = false;
    }
}

private void btnSelectAll_Click(object sender, RoutedEventArgs e)
{
    foreach (var a in authorityList)
    {
        a.IsChecked = true;
    }
}

Note that your Authority class should implement INotifyPropertyChanged to make this work.

public class Authority : INotifyPropertyChanged
{
    private string authorityName;
    private int authorityValue;
    private bool isChecked;

    public string AuthorityName
    {
        get { return authorityName; }
        set
        {
            authorityName = value;
            NotifyPropertyChanged();
        }
    }

    public int AuthorityValue
    {
        get { return authorityValue; }
        set
        {
            authorityValue = value;
            NotifyPropertyChanged();
        }
    }

    public bool IsChecked
    {
        get { return isChecked; }
        set
        {
            isChecked = value;
            NotifyPropertyChanged();
        }
    }


    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(string propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your answer Mitya.But I cant understand how to implement INotifyPropertyChanged to Authority class.Can you help me about that?
Thank you Mitya.I added the codes that you wrote.But I got errors that says "The Type 'Authority' already contains a definition for 'Ischecked' for 'AuthorityValue' and for 'AuthorityName'.What can i do for that errors?
@Baris replace your old Authority class with version I wrote
@Baris see edits again, one bracket was not in a right position
1

Implement INotifyPropertyChanged:

public class Authority : INotifyPropertyChanged
{
    private string _authorityName;
    public string AuthorityName
    {
        get { return _authorityName; }
        set { _authorityName = value; NotifyPropertyChanged(); }
    }

    private string _authorityValue;
    public string AuthorityValue
    {
        get { return _authorityValue; }
        set { _authorityValue = value; NotifyPropertyChanged(); }
    }

    private bool  _isChecked;
    public bool IsChecked
    {
        get { return _isChecked; }
        set { _isChecked = value; NotifyPropertyChanged(); }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

Set the IsChecked property of all Authority objects:

private void btnUnselectall_Click(object sender, RoutedEventArgs e)
{
    Select(false);
}

private void btnSelectAll_Click(object sender, RoutedEventArgs e)
{
    Select(true);
}

private void Select(bool select)
{
    foreach (Authority authority in authorityList)
        authority.IsChecked = select;
}

Bind the IsChecked property in your XAML:

<CheckBox Name="chkUser" IsChecked="{Binding IsChecked}" Content="{Binding AuthorityName}"/>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.