更改复选框的命中区域

时间:2012-03-02 16:41:12

标签: silverlight checkbox combobox

我正在尝试制作一个多选组合框,除了选择之外,一切似乎都正常工作,我正在使用自定义模板,包括选中的复选框,我忽略了所有标准组合框选择。

当用户专门单击复选框或其文本时,选择行有效,但是如果用户单击可见的组合框,则关闭下拉列表并且不单击复选框。我希望能够单击组合框行中的任意位置以选中复选框。

我在复选框上方放置了一个透明矩形(Hot Pink for Debugging),它取代了输入并设置了checked变量。矩形接收输入,但它仍然将click事件传递给组合框,结果选择组合框行并关闭下拉列表。

数据模板

<DataTemplate x:Key="CheckBoxTemplate">
    <Grid>
        <Rectangle VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Fill="HotPink" IsHitTestVisible="True">
             <i:Interaction.Behaviors>
                 <behaviors:HitBoxBehaviour IsChecked="{Binding IsChecked, Mode=TwoWay}"/>
             </i:Interaction.Behaviors>
         </Rectangle>
         <CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay}" Content="{Binding Name}" Tag="{Binding Id}" HorizontalAlignment="Stretch" Height="25" FontSize="14"
                              VerticalAlignment="Stretch"/>
    </Grid>
</DataTemplate>

行为

public class HitBoxBehaviour : Behavior<Rectangle>
{
    public bool IsChecked
    {
        get { return (bool)GetValue(IsCheckedProperty); }
        set { SetValue(IsCheckedProperty, value); }
    }

    public static readonly DependencyProperty IsCheckedProperty =
        DependencyProperty.Register("IsChecked", typeof(bool), typeof(HitBoxBehaviour),
        new PropertyMetadata(false, new PropertyChangedCallback(parameterChanged)));


    private static void parameterChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
    }

    protected override void OnAttached()
    {
        base.OnAttached();

        this.AssociatedObject.MouseLeftButtonDown += new MouseButtonEventHandler(AssociatedObject_MouseLeftButtonDown);
    }

    void AssociatedObject_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        IsChecked = !IsChecked;
        e.Handled = true;
    }
}

无论如何都要扩展复选框的点击框而不将点击事件传递给组合框?

2 个答案:

答案 0 :(得分:0)

这对我有用:

<强> XAML

<Border x:Name="CheckBorder" Width="100" Height="30" Background="Black" MouseLeftButtonDown="CheckBorder_MouseLeftButtonDown">
      <CheckBox x:Name="CheckBox"></CheckBox>
</Border>

<强> CS

private void CheckBorder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
     CheckBox.IsChecked = !CheckBox.IsChecked;
}

答案 1 :(得分:0)

如果您重新模板CheckBox并为其提供透明背景,您可以点击任意位置切换其复选框。不需要“hitbox”。

<Style TargetType="CheckBox">
    <Setter Property="Background" Value="#FF448DCA"/>
    <Setter Property="Foreground" Value="#FF000000"/>
    <Setter Property="HorizontalContentAlignment" Value="Left"/>
    <Setter Property="VerticalContentAlignment" Value="Top"/>
    <Setter Property="Padding" Value="4,1,0,0"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="BorderBrush">
        <Setter.Value>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="#FFA3AEB9" Offset="0"/>
                <GradientStop Color="#FF8399A9" Offset="0.375"/>
                <GradientStop Color="#FF718597" Offset="0.375"/>
                <GradientStop Color="#FF617584" Offset="1"/>
            </LinearGradientBrush>
        </Setter.Value>
    </Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="CheckBox">
                <!-- Added Background here -->
                <Grid Background="Transparent">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="16"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>