简体   繁体   English

通过单击背景在列表框中选择一个复选框

[英]Select a CheckBox in a ListBox by clicking on the background

I have a little problem with my ListBox 我的ListBox有点问题
I can check a CheckBox by clicking on the text, 我可以通过单击文本来选中一个CheckBox
But if I click on the background I can't check my CheckBox 但是,如果我单击背景,则无法检查我的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 . 因此,我该如何使用例如OnMouseLeftButtonDown处理该问题。
The item selected by the left button must set the box as check.. 使用左键选择的项目必须将复选框设置为选中。

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. 您可以将IsChecked绑定到祖先ListBoxItem的IsSelected属性。

<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). 或者,您可以更灵活地使用IValueConverter ,并传入一个标识符,例如列表的SelectedIndex +一个具有匹配intConverterParameter ,如果它们匹配则转换为true (或任何其他标识符)。

<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: 请注意,您需要在xaml中引用此参数,才能将int作为参数传递:

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. 尽管我认为您应该创建一个自己的好UserControl ,该UserControlListBox继承并根据需要进行修改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM