Bindable Property is not working in Content View .net Maui

tech 21 Reputation points
2022-05-04T16:40:15.29+00:00

I have created a content view ,added Boolean property and button to it. When I click the button button I change Boolean property, Notifychanged property triggers but UI is not updating accordingly.

String property updates working fine. Bool, Observable collections not working.

Please find sample code below

xaml:

               <Label
                Margin="0,-5,0,0"
                Padding="10,0"
                FontAttributes="Italic"
                HorizontalOptions="EndAndExpand"
                Text="{Binding IsBusy, Source={x:Reference CustomView}}" />

                 <Button IsVisible="{Binding IsBusy, Source={x:Reference CustomView}}" Text="New" />

                <Button

                Command="{Binding ChangeBoolValue, Source={x:Reference CustomView}}"
                HorizontalOptions="Center"
                Text="Change" />

Xaml.cs

public static readonly BindableProperty IsBusyProperty = BindableProperty.Create(nameof(IsBusy),
        typeof(bool), typeof(CustomViewControl),
        defaultValue: false, defaultBindingMode: BindingMode.TwoWay,
        propertyChanged: IsBusyPropertyChanged);

private static void IsBusyPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
    var control = (CustomViewControl)bindable;
    control.IsBusy = (bool)newValue;
}

public bool IsBusy
{
    get
    {
        return (bool)GetValue(IsBusyProperty);
    }
    private set
    {
        SetValue(IsBusyProperty, value);
    }
}

protected async Task ChangeBoolValue()
{
    IsBusy = true;     
 }
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,861 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 26,221 Reputation points Microsoft Vendor
    2022-05-05T09:10:57.87+00:00

    Hello,

    Try to check Source={x:Reference CustomView}, please make sure CustomView is the name of your ContentView ( CustomViewControl ), refer to the following code:
    CustomControl XAML

    <?xml version="1.0" encoding="utf-8" ?>  
    <ContentView  ......  
                  x:Name="MyCustomView">// key point  
      
        <StackLayout>  
      
            <Label  
                     Margin="0,-5,0,0"  
                     Padding="10,0"  
                     FontAttributes="Italic"  
                     HorizontalOptions="EndAndExpand"  
                     Text="{Binding IsBusy, Source={x:Reference MyCustomView}, StringFormat='The value is {0}'}"   
                />  
            <Button  IsVisible="{Binding IsBusy, Source={x:Reference MyCustomView}}"  Text="New" />  
            <Button  
                     HorizontalOptions="Center"  
                Clicked="Button_Clicked"  
                     Text="Change" />  
        </StackLayout>  
       
    </ContentView>  
    

    Click button to change the Value of IsBusy

     private void Button_Clicked(object sender, EventArgs e)  
        {  
            this.IsBusy = !this.IsBusy;  
        }  
    

    MainPage

    <?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="CustomViewMAUIDemo.MainPage"  
                 xmlns:local ="clr-namespace:CustomViewMAUIDemo">  
      
                <local:CustomView  IsBusy ="True" >  
      
                </local:CustomView>  
    </ContentPage>  
    

    Best Regards,
    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.