WPF RichTextBox with no width set

后端 未结 6 1052
旧巷少年郎
旧巷少年郎 2020-12-14 16:27

I have the following XAML code:



        
6条回答
  •  青春惊慌失措
    2020-12-14 17:06

    Just for the record as I think this thread is missing some explanations as per the why: RichTextBox MeasureOverride implementation is like that. I won't call that a bug, maybe just a poor design behavior justified by the fact that just like mentioned above the FlowDocument is not cheap to measure due to its complexity. Bottom line, avoid unlimited Width constraint by binding MinWidth or wrap it in a limiting container.

        /// 
        /// Measurement override. Implement your size-to-content logic here.
        /// 
        /// 
        /// Sizing constraint.
        /// 
        protected override Size MeasureOverride(Size constraint)
        {
            if (constraint.Width == Double.PositiveInfinity)
            {
                // If we're sized to infinity, we won't behave the same way TextBox does under
                // the same conditions.  So, we fake it.
                constraint.Width = this.MinWidth;
            }
            return base.MeasureOverride(constraint);
        }
    

提交回复
热议问题