0

The requirement is that the GridLength is to be set at the minimum value, although if the data in the text box increases (length of the text), it should set the value from the minimum to Auto.

//Right Now

 public static readonly DependencyProperty BoxWidthProperty =
        DependencyProperty.Register("BoxWidth", typeof(GridLength), typeof(ValueBox), new UIPropertyMetadata(new GridLength(80.0)));

// SomethingLike that

public static readonly DependencyProperty BoxWidthProperty =
        DependencyProperty.Register("BoxWidth", typeof(GridLength), typeof(ValueBox), new UIPropertyMetadata(new GridLength(80.0,GridUnitType.Auto)));

So, what I was trying that I should able to set the default GridLength value as 80 but in case the length of the text increases, it should be able to set the value as GridUnitType.Auto.

//Field Prop

 public GridLength BoxWidth
    {
      get
      {
        return (GridLength)GetValue(BoxWidthProperty);
      }

      set
      {
        SetValue(BoxWidthProperty, value);
      }
    }
  • Using only Grid Length is impossible to do. The maximum and minimum sizes are set by separate properties: MinHeight, MaxHeight. – EldHasp Jun 28 '21 at 07:51
  • @EldHasp , thanks I got your point. But, still, I think I would probably add an additional property that sets the width of the box. Any help in that field makes more sense. – Manav Mehta Jun 28 '21 at 10:00
  • For more specific answers, we need more information in your question. In which class do you declare this property? Better if you show its implementation in full. How do you expect to use it, bind it? – EldHasp Jun 28 '21 at 10:28

1 Answers1

0

Manav Mehta, your approach is nice. but check following url. C# WPF - GridLength GridUnitType.Auto
as you can see, the first parameter is not available when second parameter is GridUnitType.Auto.
If I am in your condition, I will use default control that WPF provide. like ContentControl, not trying to implement MinWidth, Width Dependency Property.

and I want you to consider one more thing. In UX view, if content goes grow, then parent control usually provide scrollviewer. It's special case when parent control goes grow as child control goes grow.

Jeong Yo Han
  • 551
  • 4
  • 16
  • thanks. I understand what. I was doing wrong. but still, I don't have UX view, as it is mostly coded in c#. So, I would probably add an additional property which sets the width of the box. Any help in that field makes more sense. – Manav Mehta Jun 28 '21 at 09:59