Bindable Property is not working in .Net MAUI Version 17.3.0 Preview 2.0
Sanjay Kumar Jha
156
Reputation points
I have created a ContentPage ,added Boolean property and button and ListView to it. When I click the button I change Boolean property, Notifychanged property triggers but UI is not updating accordingly.
String property updates working fine. Bool, Observable collections not working.Not idea why is it happening after taking version Update.
Please find sample code below
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="CustomControls.AppPresentation.DynamicContentChangePage"
BackgroundColor="#042C6B"
Title="DynamicContentChangePage">
<VerticalStackLayout>
<Label
Text="{Binding Labeltext}"
VerticalOptions="Center"
HorizontalOptions="Center" FontAttributes="Bold" TextColor="White" />
<ListView Margin="10,10,0,0" VerticalScrollBarVisibility="Always" x:Name="MenuList" SeparatorColor="#042C6B" SelectionMode="Single" IsVisible="True" HasUnevenRows="True" ItemsSource="{Binding DeviceListMenuItem}" VerticalOptions="FillAndExpand">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<StackLayout BackgroundColor="#042C6B" Orientation="Horizontal" x:Name="MainStackPanel" Padding="0,9,0,9" Spacing="20" Grid.Row="0" >
<Grid VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" >
<Grid.RowDefinitions/>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2"/>
<ColumnDefinition Width="32"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="32"/>
<ColumnDefinition Width="2"/>
</Grid.ColumnDefinitions>
<StackLayout Orientation="Vertical" Spacing="0" Grid.Row="0" Grid.Column="2" >
<Label Text="{Binding DeviceName}" LineBreakMode="WordWrap" TextDecorations="None" HorizontalTextAlignment="Start" TextColor="White" FontSize="19" VerticalOptions="StartAndExpand" />
<Label Text="{Binding DeviceMacAddress}" LineBreakMode="WordWrap" TextDecorations="None" HorizontalTextAlignment="Start" TextColor="White" FontSize="19" VerticalOptions="StartAndExpand" />
</StackLayout>
</Grid>
</StackLayout>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Image Source="languageimg.png" HeightRequest="50" WidthRequest="50" Margin="10" IsVisible="{Binding IsImage1Visible}"/>
<Image Source="lifttracckimg.png" HeightRequest="50" WidthRequest="50" Margin="10" IsVisible="{Binding IsImage2Visible}"/>
<Button Text="StartChange" Command="{Binding StartButtonCommand}" Margin="20"/>
</VerticalStackLayout>
</ContentPage>
Xaml.cs
public partial class DynamicContentChangePage : ContentPage
{
public DynamicContentChangePage()
{
InitializeComponent();
this.BindingContext = new DynamicContentChangePageViewModel();
}
}
DynamicContentChangePageViewModel.cs
public class DynamicContentChangePageViewModel : BindableBase
{
public DelegateCommand StartButtonCommand { get; set; }
private string _Labeltext = "Lable1";
public string Labeltext
{
get { return _Labeltext; }
set
{
_Labeltext = value;
RaisePropertyChanged();
}
}
private ObservableCollection<DeviceListMenuItemModel> _DeviceListMenuItem;
public ObservableCollection<DeviceListMenuItemModel> DeviceListMenuItem
{
get { return _DeviceListMenuItem; }
set
{
SetProperty(ref _DeviceListMenuItem, value, "DeviceListMenuItem");
}
}
private bool _IsImage1Visible=true;
public bool IsImage1Visible
{
get { return _IsImage1Visible; }
set
{
_IsImage1Visible = value;
RaisePropertyChanged();
}
}
private bool _IsImage2Visible = false;
public bool IsImage2Visible
{
get { return _IsImage2Visible; }
set
{
_IsImage2Visible = value;
RaisePropertyChanged();
}
}
public DynamicContentChangePageViewModel()
{
DeviceListMenuItem = new ObservableCollection<DeviceListMenuItemModel>();
StartButtonCommand = new DelegateCommand(ChangeEvent);
FirstMethod();
}
private void ChangeEvent()
{
SecondMethod();
Labeltext = "Lable2";
IsImage1Visible = false;
IsImage2Visible = true;
}
private void FirstMethod()
{
if (DeviceListMenuItem.Count>0)
{
DeviceListMenuItem.Clear();
}
DeviceListMenuItem.Add(new DeviceListMenuItemModel
{
DeviceId = "AE09755555",
DeviceMacAddress = "12:90:34:678",
DeviceName = "LIFTTRACK_FirstMethod_1.0",
CustomerName = "SANJAY_FirstMethod",
DeviceRssiValue = 12,
});
DeviceListMenuItem.Add(new DeviceListMenuItemModel
{
DeviceId = "AE09755555",
DeviceMacAddress = "12:90:34:678",
DeviceName = "LIFTTRACK_FirstMethod_1.0",
CustomerName = "SANJAY_FirstMethod",
DeviceRssiValue = 12,
});
DeviceListMenuItem.Add(new DeviceListMenuItemModel
{
DeviceId = "AE09755555",
DeviceMacAddress = "12:90:34:678",
DeviceName = "LIFTTRACK_FirstMethod_1.0",
CustomerName = "SANJAY_FirstMethod",
DeviceRssiValue = 12,
});
}
private void SecondMethod()
{
if (DeviceListMenuItem.Count > 0)
{
DeviceListMenuItem.Clear();
}
DeviceListMenuItem.Add(new DeviceListMenuItemModel
{
DeviceId = "AE09755555",
DeviceMacAddress = "12:90:34:678",
DeviceName = "LIFTTRACK_SecondMethod_1.0",
CustomerName = "SANJAY_SecondMethod",
DeviceRssiValue = 12,
});
DeviceListMenuItem.Add(new DeviceListMenuItemModel
{
DeviceId = "AE09755555",
DeviceMacAddress = "12:90:34:678",
DeviceName = "LIFTTRACK_SecondMethod_1.0",
CustomerName = "SANJAY_SecondMethod",
DeviceRssiValue = 12,
});
DeviceListMenuItem.Add(new DeviceListMenuItemModel
{
DeviceId = "AE09755555",
DeviceMacAddress = "12:90:34:678",
DeviceName = "LIFTTRACK_SecondMethod_1.0",
CustomerName = "SANJAY_SecondMethod",
DeviceRssiValue = 12,
});
}
}
DeviceListMenuItemModel.cs:
public class DeviceListMenuItemModel : BindableBase
{
private string _DeviceId;
public string DeviceId
{
get
{
return _DeviceId;
}
set
{
SetProperty(ref _DeviceId, value, "DeviceId");
}
}
private string _DeviceName;
public string DeviceName
{
get { return _DeviceName; }
set {SetProperty(ref _DeviceName , value, "DeviceName"); }
}
public string DeviceMacAddress { get; set; }
public int FailedCaseCount { get; set; } = 0;
public int DeviceRssiValue { get; set; }
private string _DeviceDownloadStatus = "";
public string DeviceDownloadStatus
{
get { return _DeviceDownloadStatus; }
set
{
SetProperty(ref _DeviceDownloadStatus, value, "DeviceDownloadStatus");
}
}
private bool _IsDownloadLabelVisible = true;
public bool IsDownloadLabelVisible
{
get { return _IsDownloadLabelVisible; }
set
{
SetProperty(ref _IsDownloadLabelVisible, value, "IsDownloadLabelVisible");
}
}
public bool AutoDownloadCompleteStatus { get; set; }
private string _CustomerName = "Total Valve";
public string CustomerName
{
get { return _CustomerName; }
set
{
SetProperty(ref _CustomerName, value, "CustomerName");
}
}
}
BindableBase.cs:-
public class BindableBase : INotifyPropertyChanged
{
[field: NonSerialized]
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Set the property with the specified value. If the value is not equal with the field then the field is
/// set, a PropertyChanged event is raised and it returns true.
/// </summary>
/// <typeparam name="T">Type of the property.</typeparam>
/// <param name="field">Reference to the backing field of the property.</param>
/// <param name="value">The new value for the property.</param>
/// <param name="propertyName">The property name. This optional parameter can be skipped
/// because the compiler is able to create it automatically.</param>
/// <returns>True if the value has changed, false if the old and new value were equal.</returns>
protected bool SetProperty<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
{
if (Equals(field, value)) { return false; }
field = value;
RaisePropertyChanged(propertyName);
return true;
}
/// <summary>
/// Raises the <see cref="E:PropertyChanged"/> event.
/// </summary>
/// <param name="propertyName">The property name of the property that has changed.
/// This optional parameter can be skipped because the compiler is able to create it automatically.</param>
protected void RaisePropertyChanged([CallerMemberName] string propertyName = null)
{
OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}
/// <summary>
/// Raises the <see cref="E:PropertyChanged"/> event.
/// </summary>
/// <param name="e">The <see cref="PropertyChangedEventArgs"/> instance containing the event data.</param>
protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, e);
}
}
}
Developer technologies .NET .NET MAUI
4,152 questions
Sign in to answer