2011-06-21 68 views
3

我有一個CheckBox對象,它沒有任何與它關聯的文本。 它是在一個網格中有另一個coulmn用作它的標籤。 複選框旁邊的空格可以點擊,它會切換複選框 我希望它只是實際複選框的區域而不是它周圍的空白區域(因爲它的文本設置爲空白)。更改複選框的可選區域寬度

我嘗試設置寬度= 5但這並不有所作爲

非常感謝你!

+2

確保您遵守UI約定:單擊標籤也應切換複選框,除非它是隻讀指示器。我討厭Java的安裝程序,他們想安裝雅虎工具欄,你只能點擊複選框本身,而不是標籤。所以你最好不要傳播那種混亂。 – djdanlib

回答

5

這聽起來像你的複選框延伸到網格單元(與所有的寬度可點擊的)的整個寬度,而你不希望它伸展。

將CheckBox的Horizo​​ntalAlignment屬性設置爲左(或居中或右)。然後,它將完全是複選框所需的寬度,而不是拉伸到其父級提供的整個寬度。

+0

謝謝你的工作 – user565660

+1

然後接受答案。 –

2

爲此,您必須進入複選框模板並對其進行修改。具體來說,您應該進入並刪除顯示文本的ContentPresenter。既然你沒有文字,這不是問題。最終結果將如下所示。只需將該樣式添加到您的複選框。

默認模板ContentPresenter位於Bullet下。因此,點擊該內容展示者(即使是空的,我認爲它具有默認大小)將會激活控件點擊邏輯。

<Style x:Key="CheckBoxStyle1" TargetType="{x:Type CheckBox}"> 
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> 
    <Setter Property="Background" Value="{StaticResource CheckRadioFillNormal}"/> 
    <Setter Property="BorderBrush" Value="{StaticResource CheckRadioStrokeNormal}"/> 
    <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}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" IsChecked="{TemplateBinding IsChecked}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}"/> 
        </BulletDecorator.Bullet> 
       </BulletDecorator> 
       <ControlTemplate.Triggers> 
        <Trigger Property="HasContent" Value="true"> 
         <Setter Property="FocusVisualStyle" Value="{StaticResource CheckRadioFocusVisual}"/> 
         <Setter Property="Padding" Value="2,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>