How do I make a RadioButton's Bullet align top?

前端 未结 3 1938
我寻月下人不归
我寻月下人不归 2020-12-17 19:12

I have a multiline radio button and I want the bullet to be to the left of the content (as default) aligned to the top of the radio button control. What\'s the easiest way t

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-17 19:22

    I've built a relatively generic template based on Simon Weaver's answer that can be used in most situations without having to remember to customize your RadioButton.Content all the time.

    
        
            
                
                
                    
                
            
        
    
    

    To explain how the template works:

    • The TextBlock is there because by default, the RadioButton bullet aligns with the first line of Text if the content is a Text object (a Label will not work)

    • The LineBreak is to wrap the content to a new line, so the first line is created

    • The InlineUIContainer is so we can place non-text content into a TextBlock

    • The ContentPresenter is to hold the actual content, and it has a negative top margin to remove the space left by the LineBreak object.

    Here's some example content:

    
        
            
                
        
    
        
            
                
        
    
    
        
            
                
        
    
    

    And how it looks:

    enter image description here

    The only thing I'm really not happy about is the negative top margin for the ContentPresenter being hard-coded, because if you ever change your font size, you have to manually adjust that margin.

    It probably wouldn't be that hard to build a converter for the Margin property that calculates the height of the line break ({TemplateBinding FontSize}?), or even an extended version of the RadioButton control which has this behavior by default, but for now I'm fine with hard-coding -21 based on my application's default font size.

    Also, you might want to add some TemplateBindings to the RadioButton in the Template if you want to inherit other properties from the original RadioButton, such as margins, padding, alignment, etc. I only bound to IsChecked for the purpose of keeping it simple.

提交回复
热议问题