English 中文(简体)
Databinding doesnt work in my xaml.cs file
原标题:
  • 时间:2023-06-04 10:37:33
  •  标签:
  • c#
  • xaml
  • maui

I am trying to load some data from my Model to the xaml.cs file. But it doesn t work. In my Mainpage.xaml i have a binding property inside a label. In the code behind file a BindingContext to my ViewModel und there i have a List with some data of type Do i miss something?

MainPage.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="DatabindingNavListExample.MainPage"
             xmlns:models="clr-namespace:DatabindingNavListExample.Models"
             xmlns:viewmodels="clr-namespace:DatabindingNavListExample.ViewModels">
    <ScrollView>
        <VerticalStackLayout>
            <Label FontSize="50" Text="{Binding Userdata.Username}" />
            <Label FontSize="50" Text="{Binding Userdata.Firstname}" />
            <Label FontSize="50" Text="{Binding Userdata.Lastname}" />
        </VerticalStackLayout>
    </ScrollView>
</ContentPage>

MainPage.xaml.cs

using DatabindingNavListExample.ViewModels;
namespace DatabindingNavListExample;
public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
        BindingContext = new MainPageViewModel();
    }
}

MainPageViewModel.cs

using DatabindingNavListExample.Models;

namespace DatabindingNavListExample.ViewModels {
    public class MainPageViewModel {

        public List<UserModel> Userdata { get; set; } = new();

        public MainPageViewModel() { 
            Userdata.Add(new UserModel() { Username = "test", Firstname = "John", 
            Lastname = "Doe"});
        }
    }
}

UserModel.cs

using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace DatabindingNavListExample.Models {
    public class UserModel : INotifyPropertyChanged {
    private string username;
    private string firstname;
    private string lastname;

    public string Username {
        get => username; set {
            username = value;
            OnPropertyChanged();
        }
    }
    public string Firstname {
        get => firstname; set {
            firstname = value;
            OnPropertyChanged();
        }
    }
    public string Lastname {
        get => lastname; set {
            lastname = value;
            OnPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged([CallerMemberName] string propName = null) {
        PropertyChanged?.Invoke(this,
             new PropertyChangedEventArgs(propName));
    }
}

}

问题回答

When you want to use a collection as a binding in WPF you should use the ObservableCollection<T> instead of List<T>.

The ObservableCollection<T> has the functionallity of the INotifyPropertyChanged-interface already built into it, which the regular List<T> does not.

If your List<T> would be pre-populated instead of just a new List<T>() then you would actually see some data show up in your UI. However, any changes made to the data inside that List<T> would not translate over to your UI because the UI doesn t get notified that the data inside that List has changed.

Since the Userdata only contains one instance of UserModel, you need to explicitly assign it as Jason suggested like below:

 <ScrollView>
        <VerticalStackLayout>
            <Label FontSize="50" Text="{Binding Userdata[0].Username}" /> 
            <Label FontSize="50" Text="{Binding Userdata[0].Firstname}" />
            <Label FontSize="50" Text="{Binding Userdata[0].Lastname}" />
        </VerticalStackLayout>
 </ScrollView>
  




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签