I have done reusable user control which is used few times in my project.
Usually the second row of the main grid needs to be 7 times bigger than first one, but in a particular situation it needs to be just 5 times bigger.
<Grid x:Name="mainGrid">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="7*"/>
</Grid.RowDefinitions>
...
</Grid>
I tried to set it through XAML:
<helperControls:AttachmentsGridControl x:Name="projectAttachmentsControl" ... HeightOfSecondRow="5" />
and in .cs part:
public int HeightOfSecondRow { get; set; }
public AttachmentsGridControl()
{
InitializeComponent();
if(HeightOfSecondRow > 0)
this.mainGrid.RowDefinitions[1].Height = new GridLength(HeightOfSecondRow, GridUnitType.Star);
}
But the value is not passed while constructors of controls are called. The value needs to be passed on a time when constructor is called, so I can specify how much the height of the second row needs to be and render it properly.
Instead of overwriting the
HeightOfSecondRowin the constructor, make it a dependency property of typeGridLengthwith a default value of7*. The coerce value callback will ensure that a value that is set in XAML or bound with a binding will be positive, otherwise it will be replaced with the default value.Adapt the binding of
Heightto use theHeightOfSecondRowproperty of yourAttachmentsGridControl.Then you can set the
HeightOfSecondRowas before. If you do not set it, the default value will be used.