【问题标题】:Changing the size of a checkbox and the check mark in it更改复选框的大小和其中的复选标记
【发布时间】:2012-02-23 14:47:09
【问题描述】:

我的应用程序将在带有触摸屏的计算机上运行,​​由警察在驾驶时操作。我正在使所有图形元素都变大,这样它们就可以由像我这样的香肠手指和更大的人操作。

我有一个需要出现在屏幕上的CheckBox。我需要缩放复选框的大小和更大的复选标记。我尝试通过右键单击并选择 Edit Template | 来编辑 Expression Blend 中复选框的模板。编辑副本。我设置了副本,使其适用于所有复选框。

我可以通过模板将它的高度和宽度属性绑定到控件的ActualHeight 来使框变大。但是,复选标记并没有因此而增长,并且位于左上角。至少可以这么说,它看起来不太对劲。

当我编辑模板时,这是我返回的 Xaml:

<Style TargetType="{x:Type CheckBox}">
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
    <Setter Property="Background" Value="{StaticResource CheckBoxFillNormal}"/>
    <Setter Property="BorderBrush" Value="{StaticResource CheckBoxStroke}"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="FocusVisualStyle" Value="{StaticResource EmptyCheckBoxFocusVisual}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type CheckBox}">
                <BulletDecorator Background="Transparent" SnapsToDevicePixels="true">
                    <BulletDecorator.Bullet>
                        <Microsoft_Windows_Themes:BulletChrome BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" IsChecked="{TemplateBinding IsChecked}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}" HorizontalAlignment="Center" VerticalAlignment="Center" Width="{TemplateBinding ActualHeight}" Height="{TemplateBinding ActualHeight}"/>
                    </BulletDecorator.Bullet>
                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                </BulletDecorator>
                <ControlTemplate.Triggers>
                    <Trigger Property="HasContent" Value="true">
                        <Setter Property="FocusVisualStyle" Value="{StaticResource CheckRadioFocusVisual}"/>
                        <Setter Property="Padding" Value="4,0,0,0"/>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Expression Blend 不允许我编辑 BulletChrome's 模板。 “编辑当前”和“编辑副本”选项被禁用。

如何完成我想做的事?

托尼

【问题讨论】:

    标签: wpf templates checkbox


    【解决方案1】:

    默认的 XAML 可以在 here on MSDN 中找到 BulletDecorator 的组成示例。该页面上几乎整个 XAML 块都处理 BulletDecorator 的外观和行为。

    所有 XAML 的核心是两个 Path 对象。第一个用于复选标记,第二个用于不确定标记(尽管名称“InderminateMark”在示例 XAML 中拼写错误。幸运的是,在 CheckBox 示例中的所有地方都拼写错误,所以没关系。

                <Path Visibility="Collapsed"
                      Width="7"
                      Height="7"
                      x:Name="CheckMark"
                      SnapsToDevicePixels="False"
                      StrokeThickness="2"
                      Data="M 0 0 L 7 7 M 0 7 L 7 0">
                  <Path.Stroke>
                    <SolidColorBrush Color="{DynamicResource GlyphColor}" />
                  </Path.Stroke>
                </Path>
                <Path Visibility="Collapsed"
                      Width="7"
                      Height="7"
                      x:Name="InderminateMark"
                      SnapsToDevicePixels="False"
                      StrokeThickness="2"
                      Data="M 0 7 L 7 0">
                  <Path.Stroke>
                    <SolidColorBrush Color="{DynamicResource GlyphColor}" />
                  </Path.Stroke>
                </Path>
    

    正如 H.B. 所指出的。在cmets中可以找到默认的WPF主题here.

    【讨论】:

    • MSDN 上的那些页面没有为您提供默认模板,而是虚构的示例。 (See this for defaults)
    • @H.B.谢谢,我不知道这些示例不是默认示例。这可以解释我注意到的一些不一致之处。
    • 在和我的老板讨论之后,我将进一步进行定制。他希望复选框看起来更像单选按钮,而不是圆形。他想要一个边界,然后用适当大小的深色填充以便容易看到。事实上,在您开车时,复选标记很容易错过。让他们脱颖而出很重要。
    【解决方案2】:

    不完美,但它有效。您设置 TextBlock 的样式(例如 FontSize、Height ...),复选标记随 TextBlock 一起增长。

    <StackPanel Name="CheckBoxPanel" Orientation="Horizontal">
        <Viewbox Height="{Binding Path=ActualHeight, ElementName=CheckBoxPanel}">
            <CheckBox />
        </Viewbox>
        <TextBlock /> <!-- for CheckBox Content -->
    </StackPanel>
    

    【讨论】:

    • 使用 Viewbox 比批准的答案要好得多。
    【解决方案3】:

    如果您更改大小,那么您还必须更改路径,路径固定为大小。如果您已经有一条路径并且只想对其进行缩放,则可以按比例增加所有数值。

    我最近需要将MSDN的示例自定义复选框[x]样式改为勾号(https://msdn.microsoft.com/en-us/library/ms752319(v=vs.85).aspx),这里的路径是前后。

    <Path Visibility="Collapsed"
        Width="7"
        Height="7"
        x:Name="CheckMark"
        SnapsToDevicePixels="False"
        StrokeThickness="2"
        Data="M 0 0 L 7 7 M 0 7 L 7 0">
        <Path.Stroke>
            <SolidColorBrush Color="{DynamicResource GlyphColor}" />
        </Path.Stroke>
    </Path>
    

    <Path Visibility="Collapsed"
        Width="9"
        Height="9"
        x:Name="CheckMark"
        SnapsToDevicePixels="False"
        StrokeThickness="2"
        Data="M 1 4.5 L 4 7.5 M 4 7.5 L 8 1">
        <Path.Stroke>
            <SolidColorBrush Color="{DynamicResource GlyphColor}" />
        </Path.Stroke>
    </Path>
    

    这两个示例都有一条路径,其中两条线使用 XY 坐标作为它们的开始和结束位置。请注意,在第一张图片中,数字范围从 0 到 7,大小为 7x7。另请注意,在第二张图片(勾号)中,我将尺寸增加到 9x9。

    我所做的只是调整每条线的开始和结束 vector2 位置的数字以获得刻度形状。

    手动编辑的替代方法是下载“Microsoft Expression Design 4”的免费版本。然后,您可以制作所需大小的新图像,然后使用 EXPORT 功能并选择 XAML 文件格式,然后检查生成的文件并从中获取路径。

    【讨论】:

      【解决方案4】:

      您要查找的是 Path,它实际上是复选标记,需要对其进行编辑。如果你查看this article's section on custom checkbox work in WPF,它基本上向你展示了如何非常简单地做到这一点。如果你想要更多的例子,我可以在今天晚些时候下班后很快踢出一个。干杯!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-09-09
        • 1970-01-01
        • 2012-02-05
        • 2018-07-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多