C
C#8mo ago
Ikarmus

❔ MAUI IsVisible Binding doesn't work

Hi, I created simple page with my custom control, which I'd like to hide conditionally (resolved in VM constructor). However setting this value doesn't change the actual visibility, XAML binding failures are stacking up, but I don't know what is wrong here
2 Replies
Ikarmus
Ikarmus8mo ago
Custom control
<?xml version="1.0" encoding="utf-8" ?>
<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:model="clr-namespace:SMCEBI_Navigator.Models"
xmlns:pc="clr-namespace:MapBuilder_API_Base;assembly=MapBuilder_API_Base"
x:Class="SMCEBI_Navigator.CustomControls.SizePicker">
<ContentView.BindingContext>
<x:Array Type="{x:Type pc:PointClass}" />
</ContentView.BindingContext>
<VerticalStackLayout>
<?xml version="1.0" encoding="utf-8" ?>
<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:model="clr-namespace:SMCEBI_Navigator.Models"
xmlns:pc="clr-namespace:MapBuilder_API_Base;assembly=MapBuilder_API_Base"
x:Class="SMCEBI_Navigator.CustomControls.SizePicker">
<ContentView.BindingContext>
<x:Array Type="{x:Type pc:PointClass}" />
</ContentView.BindingContext>
<VerticalStackLayout>
(It shouldn't matter anyway) Page XAML
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="SMCEBI_Navigator.Views.FeatureEditorPage"
xmlns:vm="clr-namespace:SMCEBI_Navigator.ViewModels"
xmlns:cc="clr-namespace:SMCEBI_Navigator.CustomControls"
x:DataType="vm:FeatureEditorViewModel"
Title="{Binding PageTitle}">
<VerticalStackLayout Padding="10">
<cc:StylePicker Padding="0,15,0,5" IsVisible="{Binding IsStylePickerVisible}"
BindingContext="{Binding EditorElement.Style}" />
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="SMCEBI_Navigator.Views.FeatureEditorPage"
xmlns:vm="clr-namespace:SMCEBI_Navigator.ViewModels"
xmlns:cc="clr-namespace:SMCEBI_Navigator.CustomControls"
x:DataType="vm:FeatureEditorViewModel"
Title="{Binding PageTitle}">
<VerticalStackLayout Padding="10">
<cc:StylePicker Padding="0,15,0,5" IsVisible="{Binding IsStylePickerVisible}"
BindingContext="{Binding EditorElement.Style}" />
ViewModel
internal partial class FeatureEditorViewModel : ObservableObject
{
[ObservableProperty] public bool isSizePickerVisible = true;
[ObservableProperty] public bool isStylePickerVisible = true;

public FeatureEditorViewModel(IDictionary<string, object> query)
{
// Unparsing query
PrepareContent(query[nameof(Type)] as Type);
}
private void PrepareContent()
{
// ...
IsSizePickerVisible = false;
IsStylePickerVisible = false;
}
}
internal partial class FeatureEditorViewModel : ObservableObject
{
[ObservableProperty] public bool isSizePickerVisible = true;
[ObservableProperty] public bool isStylePickerVisible = true;

public FeatureEditorViewModel(IDictionary<string, object> query)
{
// Unparsing query
PrepareContent(query[nameof(Type)] as Type);
}
private void PrepareContent()
{
// ...
IsSizePickerVisible = false;
IsStylePickerVisible = false;
}
}
I'm 100% sure PrepareContent is being executed
public partial class FeatureEditorPage : ContentPage, IQueryAttributable
{
public FeatureEditorPage(Dictionary<string, object> query)
{
InitializeComponent();
BindingContext = new VM(query);
InvalidateMeasure();
}
public partial class FeatureEditorPage : ContentPage, IQueryAttributable
{
public FeatureEditorPage(Dictionary<string, object> query)
{
InitializeComponent();
BindingContext = new VM(query);
InvalidateMeasure();
}
Also my code-behind I added InvalidateMeaseure because of this stack answer I came up with 10-min-fix, the most ugly workaround possible
private void StylePicker_Loaded(object sender, EventArgs e)
{
(sender as StylePicker).IsVisible = ((VM)BindingContext).IsStylePickerVisible;
}
private void StylePicker_Loaded(object sender, EventArgs e)
{
(sender as StylePicker).IsVisible = ((VM)BindingContext).IsStylePickerVisible;
}
But (as you can guess) it's totally not the way it should work
Accord
Accord8mo ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.