I'm having trouble binding one control to another. Why?

51 Views Asked by At

In my XAML file, the content page tag looks like this:

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:viewmodel="clr-namespace:Tester.ViewModel"
             x:DataType="viewmodel:Viewmodel"
             x:Class="Tester.View.NewDb"
             BackgroundColor="DarkGreen">

<ContentPage.Resources>
    <toolkit:InvertedBoolConverter x:Key="InvertedBoolConverter" />
</ContentPage.Resources>

So I have a class called Viewmodel that I'm using for data binding.

I'm trying to bind an entry control to a checkbox control like this:

<Entry Placeholder="Enter Database Password"
   Grid.Row="2"
   Margin="20,0,20,0"
   TextColor="White"
   x:Name="dbPasswd"
   BindingContext="{x:Reference chkShowPasswd}"
   IsPassword="{Binding IsChecked, Converter={StaticResource InvertedBoolConverter}}"/>
<HorizontalStackLayout
   Grid.Row="2"
   Grid.Column="1"
   HorizontalOptions="Center">
   <CheckBox x:Name="chkShowPasswd"
      VerticalOptions="Center"/>
   <Label Text="Show Password"
      TextColor="White"
      FontSize="Micro"
      VerticalOptions="Center"/>
</HorizontalStackLayout>

And when I run the program I get an error message saying:

Binding: Property "IsChecked" not found on Tester.ViewModel.Viewmodel.cs

Now I do have more entry controls in here and I am looking to bind them to ObservableObjects in the Viewmodel code-behind as well as other tasks and methods.

So my question is, how do I bind the two controls I showed in the viewmodel?

1

There are 1 best solutions below

1
Cfun On

Specify the reference to your CheckBox in the Source of your binding:

<Entry x:Name="dbPasswd"
       Grid.Row="2"
       Margin="20,0,20,0"
       IsPassword="{Binding IsChecked, Source={x:Reference chkShowPasswd},
                   Converter={StaticResource InvertedBoolConverter}}"
       Placeholder="Enter Database Password"
       TextColor="White" />