没有标签的复选框

问题描述

我正在尝试使用没有标签的 CheckBox。我曾期望我只让 Content 属性为空,并且只会获得 CheckBox Border,但总是有一个空间用于包含可能的标签。当我为背景着色时,您可以看到它: [见附件,我还没有权限直接发图片到正文][1]

然后我查看了 CheckBox.axaml。似乎不可能有一个没有标签的 CheckBox

  <ControlTemplate>
    <Grid x:Name="RootGrid" ColumnDeFinitions="20,*">
      <Border x:Name="PART_Border"
              Grid.ColumnSpan="2"
              Background="{TemplateBinding Background}"
              BorderBrush="{TemplateBinding BorderBrush}"
              BorderThickness="{TemplateBinding BorderThickness}" />

      <Grid VerticalAlignment="Top" Height="32">
        <Border x:Name="normalRectangle"
            BorderThickness="{DynamicResource CheckBoxBorderThemeThickness}"
            UseLayoutRounding="False"
            Height="20"
            Width="20" />

        <ViewBox UseLayoutRounding="False">
          <Panel>
            <Panel Height="16" Width="16" />
            <Path x:Name="CheckGlyph" Stretch="Uniform" VerticalAlignment="Center" />
          </Panel>
        </ViewBox>
      </Grid>
      <ContentPresenter x:Name="ContentPresenter"
                     ContentTemplate="{TemplateBinding ContentTemplate}"
                     Content="{TemplateBinding Content}"
                     Margin="{TemplateBinding Padding}"
                     HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                     VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                     Grid.Column="1" />
      <!-- Todo: textwrapping="Wrap" on contentpresenter -->
    </Grid>
  </ControlTemplate>

至少我看不到禁用标签的任何处理。但我真的很惊讶。我只想在 ListBox 中使用 CheckBox。我不敢相信我会是第一个想要使用没有标签的 CheckBox 的人。

我能做什么?我是 AvaloniaUI 的新手 - 抱歉我错过了一些明显的东西。 [1]:https://i.stack.imgur.com/MGcWW.png

解决方法

您始终可以像这样隐藏 ContentPresenter

<Window.Styles>
    <Style Selector="CheckBox.noContent /template/ ContentPresenter#ContentPresenter">
        <Setter Property="IsVisible" Value="False"/>
    </Style>
</Window.Styles>

<StackPanel Background="Red">
    <CheckBox Background="Blue" Content="Test1" IsChecked="True"></CheckBox>
        
    <CheckBox Classes="noContent" Content="Test2" Background="Green" IsChecked="True"></CheckBox>

    <CheckBox Classes="noContent" Content="Test3" MinWidth="10" Background="Orange" IsChecked="True"></CheckBox>
</StackPanel>

这将隐藏每个具有 ContentPresenterCheckBoxClasses="noContent"

但是,从您的屏幕截图中,我认为您当前的问题是 CheckBox 有一个 MinWidth=120,因此 Background 将比 Box。正如您所看到的 hereRectangle 本身只有一个 Width=10。因此,如果您设置 MinWidth=10,CheckBox 将不会比 Rectangle 宽:

<CheckBox Classes="noContent" Content="Test3" MinWidth="10" Background="Orange" IsChecked="True"></CheckBox>

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...