Quantcast
Channel: Active questions tagged listview - Stack Overflow
Viewing all articles
Browse latest Browse all 169

MAUI BindingContext does not work when set to a Binding

$
0
0

On my main page I have a MainViewModel that contains some properties, one of which is EffectsCtrl (another view model, instance of EffectsControl class), which contains some logic associated with managing the list of effects. To display this list I have the following ListView on my main page:

<ContentPage  xmlns="http://schemas.microsoft.com/dotnet/2021/maui"  xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  xmlns:local="clr-namespace:Effects"  x:Class="Effects.MainPage"  xmlns:vm="clr-namespace:Effects.ViewModel"  x:DataType="vm:MainViewModel">  ...<ListView    ItemsSource="{Binding EffectsCtrl.Effects}"    HasUnevenRows="True"    VerticalOptions="FillAndExpand"><ListView.ItemTemplate>      ...</ListView.ItemTemplate></ListView></ContentPage>

The above works fine - list properly displays and updates on changes.

But at some point I changed it to this

<ListView  BindingContext={Binding EffectsCtrl}  ItemsSource="{Binding Effects}"  HasUnevenRows="True"  VerticalOptions="FillAndExpand">  ...</ListView>

and to my great surprise I was shown a tip by my IDE (Visual Studio) that Effects 'Member not found in data context MainViewModel' and upon launching the build failed with an error 'Binding: Property "Effects" not found on "Effects.ViewModel.MainViewModel"'.At this point I was a little confused and curious so I tried some other combinations like

<ListView  BindingContext={Binding EffectsCtrl}  ItemsSource="{Binding EffectsCtrl.Effects}"  HasUnevenRows="True"  VerticalOptions="FillAndExpand">  ...</ListView>

and this compiles fine without warnings from IDE or build errors but the list does not display at all - I assume due to wrong binding because I checked the EffectsCtrl in debugger and everything is fine and elements are added to the list as usual. Also there are no warnings or errors displayed by the debugger.

And this brings me to my question: is this a bug or do I misunderstand something about binding? When I was making the first change I assumed that when I change the ListView's BindingContext it will affect the binding on its ItemsSource field. And I'm even more confused because the 3rd case seems to somewhat confirm my assumption. So can someone confirm this is a bug or provide me with an explanation why the 2nd case is invalid?

EDIT:I did some more testing and when x:DataType="vm:MainViewModel" from the ContentPage declaration is removed the 2nd case works as intended. Why is that?


Viewing all articles
Browse latest Browse all 169

Latest Images

Trending Articles





Latest Images