mercredi 17 avril 2019

Check ckeckbox in wpf by clicking on the whole item

My client has got the problem with the size of the checkbox inside a listbox. I agree, it's small and not so easy to check at times.

I've tried to find a way to make a checkbox bigger but I've found out that it's complicated (and would require using Blend, which I don't want to use).

What I want to do though is to check the checkbox when clicking on a whole item.

[ ] some text

In this example - on "some text" or inside the item wherever. Right now I have to click inside the checkbox to have it checked.

My xamls of this control looks like this:

     <ListBox Name="restoredDBsListBox" ItemsSource="{Binding ProperlyRestoredDatabases}"  Grid.ColumnSpan="2" HorizontalAlignment="Left" Height="170" Margin="34,160,0,0" VerticalAlignment="Top" Width="276" SelectionMode="Extended">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal" >
                    <CheckBox Name="check" IsChecked="{Binding IsChecked, Mode=TwoWay}" Margin="3" VerticalAlignment="Center"/>
                    <ContentPresenter Content="{Binding Value}" Margin="1"/>                       
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

and my ViewModel:

    /// <summary>
    /// Defines the _properlyRestoredDatabases
    /// </summary>
    private CheckableObservableCollection<string> _properlyRestoredDatabases;

    /// <summary>
    /// Gets or sets the ProperlyRestoredDatabases
    /// </summary>
    public CheckableObservableCollection<string> ProperlyRestoredDatabases
    {
        get { return _properlyRestoredDatabases; }
        set
        {
            _properlyRestoredDatabases = value;
            OnPropertyChanged("ProperlyRestoredDatabases");
        }
    }

CheckableObservableCollection class :

  public class CheckableObservableCollection<T> : ObservableCollection<CheckedWrapper<T>>
{
    private ListCollectionView _selected;

    public CheckableObservableCollection()
    {
        _selected = new ListCollectionView(this);
        _selected.Filter = delegate (object checkObject) {
            return ((CheckedWrapper<T>)checkObject).IsChecked;
        };
    }

    public void Add(T item)
    {
        this.Add(new CheckedWrapper<T>(this) { Value = item });
    }

    public ICollectionView CheckedItems
    {
        get { return _selected; }
    }

    internal void Refresh()
    {
        _selected.Refresh();
    }
}

and CheckedWrapper

 public class CheckedWrapper<T> : INotifyPropertyChanged
{
    private readonly CheckableObservableCollection<T> _parent;

    public CheckedWrapper(CheckableObservableCollection<T> parent)
    {
        _parent = parent;
    }

    private T _value;

    public T Value
    {
        get { return _value; }
        set
        {
            _value = value;
            OnPropertyChanged("Value");
        }
    }

    private bool _isChecked;

    public bool IsChecked
    {
        get { return _isChecked; }
        set
        {
            _isChecked = value;
            CheckChanged();
            OnPropertyChanged("IsChecked");
        }
    }

    private void CheckChanged()
    {
        _parent.Refresh();
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler pceh = PropertyChanged;
        if (pceh != null)
        {
            pceh(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}




Aucun commentaire:

Enregistrer un commentaire