How to set WPF Window's width equal to the content in its Title Bar?

前端 未结 1 1698
挽巷
挽巷 2020-12-21 04:15

If I have Window in WPF as follows:



        
相关标签:
1条回答
  • 2020-12-21 04:33

    Add a hidden textblock in to the window:

    <TextBlock 
       Text="{Binding Path=Title,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}" 
       Visibility="Hidden" 
       Height="0" 
       Margin="100 0 0 0">
    </TextBlock>
    

    The Margin allows for the windows controls to be pushed out of the way.

    The Height makes the control take up no vertical space.

    The Visiblity is probable not required because of the zero height, but by setting it to hidden will cause it to take up space on the canvas but show nothing.

    0 讨论(0)
提交回复
热议问题