WPF C#复选框可点击区域的修改 - 批 [英] WPF c# Checkbox clickable area modification - BATCH

查看:745
本文介绍了WPF C#复选框可点击区域的修改 - 批的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大约3000个复选框实例,您可以点击整行,复选框的状态将会改变。

I have about 3000 instances of checkboxes that are styled in a way which you can click the entire row and the state of the checkboxes will change.

修改复选框,以便只有框是可以点击。

How do you modify the checkbox so that ONLY the box is click able. I did a good amount of search and arrived at no conclusion.

感谢

推荐答案

您必须做的一切是检查点击了哪个元素。

Everything you must do is check which element has been clicked.

在第一个解决方案中,你应该检查OriginalSource是否 BulletChrome ,如果是,你应该允许CheckBox更改状态,通过设置: e.Handled = false; 。如果您不允许更改CheckBox状态,您应该将 e.Handled 设置为true。

In the first solution you should check if OriginalSource is BulletChrome and if it is, you should allow CheckBox to change state, by setting: e.Handled = false;. If you don't allow to change CheckBox state you should set e.Handled to true.

解决方案你应该检查OriginalSource是边框路径,并确保相应的元素被点击,我们应该检查

In the second solution you should check if OriginalSource is Border or Path and to make sure that the appropriate element was clicked, we should check names.

您可以使用 PreviewMouseLeftButtonDown 事件来确定OriginalSource。

You can use PreviewMouseLeftButtonDown event, to determine OriginalSource.

XAML:

<CheckBox Content="test" PreviewMouseLeftButtonDown="CheckBox_PreviewMouseLeftButtonDown"/>

代码隐藏:

private void CheckBox_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    if (e.OriginalSource is BulletChrome)
        e.Handled = false;
    else
        e.Handled = true;
}



或者您可以创建自己的CheckBox ControlTemplate( msdn example )。

您应该检入 PreviewMouseLeftButtonDown 事件OriginalSource并允许或拒绝更改复选框状态。

You should check in PreviewMouseLeftButtonDown event OriginalSource and allow or deny change CheckBox state.

PreviewMouseLeftButtonDown

<Window.Resources>
    <Style x:Key="CheckBoxFocusVisual">
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate>
                    <Border>
                        <Rectangle 
        Margin="15,0,0,0"
        StrokeThickness="1"
        Stroke="#60000000"
        StrokeDashArray="1 2"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <LinearGradientBrush x:Key="NormalBrush" StartPoint="0,0" EndPoint="0,1">
        <GradientBrush.GradientStops>
            <GradientStopCollection>
                <GradientStop Color="#FFF" Offset="0.0"/>
                <GradientStop Color="#CCC" Offset="1.0"/>
            </GradientStopCollection>
        </GradientBrush.GradientStops>
    </LinearGradientBrush>
    <LinearGradientBrush x:Key="HorizontalNormalBrush" StartPoint="0,0" EndPoint="1,0">
        <GradientBrush.GradientStops>
            <GradientStopCollection>
                <GradientStop Color="#FFF" Offset="0.0"/>
                <GradientStop Color="#CCC" Offset="1.0"/>
            </GradientStopCollection>
        </GradientBrush.GradientStops>
    </LinearGradientBrush>
    <LinearGradientBrush x:Key="LightBrush" StartPoint="0,0" EndPoint="0,1">
        <GradientBrush.GradientStops>
            <GradientStopCollection>
                <GradientStop Color="#FFF" Offset="0.0"/>
                <GradientStop Color="#EEE" Offset="1.0"/>
            </GradientStopCollection>
        </GradientBrush.GradientStops>
    </LinearGradientBrush>
    <LinearGradientBrush x:Key="HorizontalLightBrush" StartPoint="0,0" EndPoint="1,0">
        <GradientBrush.GradientStops>
            <GradientStopCollection>
                <GradientStop Color="#FFF" Offset="0.0"/>
                <GradientStop Color="#EEE" Offset="1.0"/>
            </GradientStopCollection>
        </GradientBrush.GradientStops>
    </LinearGradientBrush>
    <LinearGradientBrush x:Key="DarkBrush" StartPoint="0,0" EndPoint="0,1">
        <GradientBrush.GradientStops>
            <GradientStopCollection>
                <GradientStop Color="#FFF" Offset="0.0"/>
                <GradientStop Color="#AAA" Offset="1.0"/>
            </GradientStopCollection>
        </GradientBrush.GradientStops>
    </LinearGradientBrush>
    <LinearGradientBrush x:Key="PressedBrush" StartPoint="0,0" EndPoint="0,1">
        <GradientBrush.GradientStops>
            <GradientStopCollection>
                <GradientStop Color="#BBB" Offset="0.0"/>
                <GradientStop Color="#EEE" Offset="0.1"/>
                <GradientStop Color="#EEE" Offset="0.9"/>
                <GradientStop Color="#FFF" Offset="1.0"/>
            </GradientStopCollection>
        </GradientBrush.GradientStops>
    </LinearGradientBrush>
    <SolidColorBrush x:Key="DisabledForegroundBrush" Color="#888" />
    <SolidColorBrush x:Key="DisabledBackgroundBrush" Color="#EEE" />
    <SolidColorBrush x:Key="WindowBackgroundBrush" Color="#FFF" />
    <SolidColorBrush x:Key="SelectedBackgroundBrush" Color="#DDD" />
    <LinearGradientBrush x:Key="NormalBorderBrush" StartPoint="0,0" EndPoint="0,1">
        <GradientBrush.GradientStops>
            <GradientStopCollection>
                <GradientStop Color="#CCC" Offset="0.0"/>
                <GradientStop Color="#444" Offset="1.0"/>
            </GradientStopCollection>
        </GradientBrush.GradientStops>
    </LinearGradientBrush>
    <LinearGradientBrush x:Key="HorizontalNormalBorderBrush" StartPoint="0,0" EndPoint="1,0">
        <GradientBrush.GradientStops>
            <GradientStopCollection>
                <GradientStop Color="#CCC" Offset="0.0"/>
                <GradientStop Color="#444" Offset="1.0"/>
            </GradientStopCollection>
        </GradientBrush.GradientStops>
    </LinearGradientBrush>
    <LinearGradientBrush x:Key="DefaultedBorderBrush" StartPoint="0,0" EndPoint="0,1">
        <GradientBrush.GradientStops>
            <GradientStopCollection>
                <GradientStop Color="#777" Offset="0.0"/>
                <GradientStop Color="#000" Offset="1.0"/>
            </GradientStopCollection>
        </GradientBrush.GradientStops>
    </LinearGradientBrush>
    <LinearGradientBrush x:Key="PressedBorderBrush" StartPoint="0,0" EndPoint="0,1">
        <GradientBrush.GradientStops>
            <GradientStopCollection>
                <GradientStop Color="#444" Offset="0.0"/>
                <GradientStop Color="#888" Offset="1.0"/>
            </GradientStopCollection>
        </GradientBrush.GradientStops>
    </LinearGradientBrush>
    <SolidColorBrush x:Key="DisabledBorderBrush" Color="#AAA" />
    <SolidColorBrush x:Key="SolidBorderBrush" Color="#888" />
    <SolidColorBrush x:Key="LightBorderBrush" Color="#AAA" />
    <SolidColorBrush x:Key="GlyphBrush" Color="#444" />
    <SolidColorBrush x:Key="LightColorBrush" Color="#DDD" />
    <Style x:Key="{x:Type CheckBox}" TargetType="CheckBox">
        <Setter Property="SnapsToDevicePixels" Value="true"/>
        <Setter Property="OverridesDefaultStyle" Value="true"/>
        <Setter Property="FocusVisualStyle"    Value="{StaticResource CheckBoxFocusVisual}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="CheckBox">
                    <BulletDecorator Background="Transparent" PreviewMouseLeftButtonDown="BulletDecorator_PreviewMouseLeftButtonDown">
                        <BulletDecorator.Bullet>
                            <Border x:Name="chbStyleBorder"  
                              Width="13" 
                              Height="13" 
                              CornerRadius="0" 
                              Background="{StaticResource NormalBrush}"
                              BorderThickness="1"
                              BorderBrush="{StaticResource NormalBorderBrush}">
                                <Path 
                                    Width="7" Height="7" 
                                    x:Name="chbStyleCheckMark"
                                    SnapsToDevicePixels="False" 
                                    Stroke="{StaticResource GlyphBrush}"
                                    StrokeThickness="2"
                                    Data="M 0 0 L 7 7 M 0 7 L 7 0" />
                            </Border>
                        </BulletDecorator.Bullet>
                        <ContentPresenter Margin="4,0,0,0"
                            VerticalAlignment="Top"
                            HorizontalAlignment="Left"
                            RecognizesAccessKey="True" />
                    </BulletDecorator>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsChecked" Value="false">
                            <Setter TargetName="chbStyleCheckMark" Property="Visibility" Value="Collapsed"/>
                        </Trigger>
                        <Trigger Property="IsChecked" Value="{x:Null}">
                            <Setter TargetName="chbStyleCheckMark" Property="Data" Value="M 0 7 L 7 0" />
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter TargetName="chbStyleBorder" Property="Background" Value="{StaticResource DarkBrush}" />
                        </Trigger>
                        <Trigger Property="IsPressed" Value="true">
                            <Setter TargetName="chbStyleBorder" Property="Background" Value="{StaticResource PressedBrush}" />
                            <Setter TargetName="chbStyleBorder" Property="BorderBrush" Value="{StaticResource PressedBorderBrush}" />
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter TargetName="chbStyleBorder" Property="Background" Value="{StaticResource DisabledBackgroundBrush}" />
                            <Setter TargetName="chbStyleBorder" Property="BorderBrush" Value="{StaticResource DisabledBorderBrush}" />
                            <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

代码隐藏:

private void BulletDecorator_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    if ((e.OriginalSource is Border && ((Border)e.OriginalSource).Name == "chbStyleBorder") || (e.OriginalSource is Path && ((Path)e.OriginalSource).Name == "chbStyleCheckMark"))
        e.Handled = false;
    else
        e.Handled = true;
}

这篇关于WPF C#复选框可点击区域的修改 - 批的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆