C0ZEN
C0ZEN

Reputation: 934

Select a CheckBox in a ListBox by clicking on the background

I have a little problem with my ListBox
I can check a CheckBox by clicking on the text,
But if I click on the background I can't check my CheckBox
That make me feel so bad...

I suppose that I can create a method to solve my problem,
But I would like to know if there a property to fix this easily.
Thanks for helping.

<ListBox x:Name="DeleteEntreesListBox">
    <CheckBox x:Name="myTextBox" Content="Hello"/>
</ListBox>

If my cursor is on the text the box is gray and can be check. Screen
If my cursor is not on the text but on the background the box is white and can't be check Screen

EDIT : It seems that there are no property to fix that.
So how could I do to handle that with for example a OnMouseLeftButtonDown.
The item selected by the left button must set the box as check..

Upvotes: 2

Views: 1938

Answers (1)

Sagiv b.g
Sagiv b.g

Reputation: 31024

try setting the width same as the list box (or any other parent)

<ListBox x:Name="DeleteEntreesListBox">
    <CheckBox x:Name="myTextBox" Content="Hello" Width="{Binding ActualWidth,ElementName=DeleteEntreesListBox}"/>
</ListBox>

*EDIT

You can bind IsChecked to the IsSelected Property of the ancestor ListBoxItem.

<CheckBox x:Name="myTextBox2" Content="Hello" 
          IsChecked="{Binding RelativeSource={RelativeSource FindAncestor, 
          AncestorType={x:Type ListBoxItem}}, 
          Path=IsSelected}"/>

Or you can use IValueConverter with more flexibility and pass in an identifier like the SelectedIndex of the list + a ConverterParameter with a matching int and convert to true if they match (or any other identifier).

<ListBox x:Name="DeleteEntreesListBox">
    <CheckBox x:Name="myTextBox1" Content="Hello">
        <CheckBox.IsChecked>
            <Binding Path="SelectedIndex" ElementName="DeleteEntreesListBox" Converter="{StaticResource IndexToBoolConverter}">
                <Binding.ConverterParameter>
                    <sys:Int32>0</sys:Int32>
                </Binding.ConverterParameter>
            </Binding>
        </CheckBox.IsChecked>
    </CheckBox>
</ListBox>

Notice that you'll need to reference this in your xaml in order to pass an int as a parameter:

xmlns:sys="clr-namespace:System;assembly=mscorlib"

and the converter can be something like this:

class IndexToBoolConverter : IValueConverter
{
    #region IValueConverter Members

    object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
            if (parameter != null && (int)value == (int)parameter)
            {
                return true;
            }
            else
                return false;
    }

    object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if ((bool)value == true)
            return false;
        else
            return true;
    }

    #endregion
}

though I think you should create a nice UserControl of your own that inherits from ListBox and modify it as you wish.

Upvotes: 1

Related Questions