Issue
I have a common ContentView which has been called in many ContentPages. Different pages have different requirements, according to the requirement, I want to change ColumnDefinition width property.
For Example:
On one page, There are three controls inside the Grid view, and the widths are ",1,"
<Grid ColumnDefinitions="*,1,*">
but on the other pages, I want to turn off the visibility of the first two controls, that is why I want to assign a width to Auto for the first two controls.
<Grid ColumnDefinitions="Auto,Auto,*">
In order to achieve this functionality, I have created bindable property.
public static readonly BindableProperty GridColumnDefinitionProperty =
BindableProperty.Create(nameof(GridColumnDefinition), typeof(ColumnDefinitionCollection), typeof(PopupView), "*,1,*");
public ColumnDefinitionCollection GridColumnDefinition
{
get => (ColumnDefinitionCollection)GetValue(GridColumnDefinitionProperty);
set => SetValue(GridColumnDefinitionProperty, value);
}
but I am getting this error
System.ArgumentException: 'Default value did not match return type. Property: Xamarin.Forms.ColumnDefinitionCollection PopupView.GridColumnDefinition Default value type: String, Parameter name: defaultValue'
I am getting this error at the Bindable property line.
Solution
For simple situations, an alternative approach is to create the needed ColumnDefinitions in code behind. With a bool
property controlling when to do so.
<Grid x:Name="theGrid" ... />
public static readonly BindableProperty UseAutoColumnsProperty =
BindableProperty.Create(nameof(UseAutoColumns), typeof(bool), typeof(ContentPage), false);
public bool UseAutoColumns
{
get => (bool)GetValue(UseAutoColumnsProperty);
set
{
SetValue(UseAutoColumnsProperty, value);
if (value)
theGrid.ColumnDefinitions = new ColumnDefinitionCollection
{
new ColumnDefinition(GridLength.Auto),
new ColumnDefinition(GridLength.Auto),
new ColumnDefinition(GridLength.Star),
};
}
}
Answered By - ToolmakerSteve
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.