最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Binding an ObservableCollection within a datagrid's Template Column - Stack Overflow

programmeradmin0浏览0评论

I have a datagrid and a class I wrote a class called HyperlinkLabel in the MainPage.cs file. I have the datagrid data bound to an observable collection in a view model. the problem that I have is the first column of the datagrid is blank is blank but all other fields are filled. here is my code.

XAML:

<ContentPage xmlns=";
             xmlns:viewmodel="clr-namespace:LockAndKeyMaui.ViewModels"
             xmlns:sf="clr-namespace:Syncfusion.Maui.DataGrid;assembly=Syncfusion.Maui.DataGrid"
             xmlns:x=";
             xmlns:local="clr-namespace:LockAndKeyMaui.Views"
             x:Class="LockAndKeyMaui.Views.MainPage"
             x:DataType="viewmodel:MainViewModel">

        <sf:SfDataGrid
           x:Name="PswdList"
           AutoGenerateColumnsMode="None"
           SelectionMode="Multiple"
           NavigationMode="Row" SortingMode="Single"
           BackgroundColor="White" HeaderRowHeight="30"
           HeaderGridLinesVisibility="Both" GridLinesVisibility="Both"
           IsVisible="{Binding PwVisible}" MinimumHeightRequest="0"
            ItemsSource="{Binding PswdGrp}">
            <sf:SfDataGrid.Columns>
                <sf:DataGridTemplateColumn
                    MappingName="SiteAddr" HeaderText="Title"
                    HeaderTextAlignment="Center" Width="208">
                    <sf:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <local:HyperlinkLabel
                                Text="{Binding Title}" Url="{Binding SiteAddr}"
                                VerticalOptions="Center"/>
                        </DataTemplate>
                    </sf:DataGridTemplateColumn.CellTemplate>
                </sf:DataGridTemplateColumn>
                <sf:DataGridTextColumn
                    MappingName="UsrName" HeaderText="User Name"
                    HeaderTextAlignment="Center" Width="207"/>
                <sf:DataGridTextColumn
                    MappingName="Passwrd" HeaderText="Password"
                    HeaderTextAlignment="Center" Width="206"/>
            </sf:SfDataGrid.Columns>
        </sf:SfDataGrid>

The class in the code-behind of the XAML file:

public partial class HyperlinkLabel : Label
{
    public static readonly BindableProperty UrlProperty = BindableProperty.Create(nameof(Url),
        typeof(string), typeof(HyperlinkLabel), null);

    public string Url
    {
        get { return (string)GetValue(UrlProperty); }
        set { SetValue(UrlProperty, value); }
    }

    public HyperlinkLabel()
    {
        TextDecorations = TextDecorations.Underline;
        TextColor = Colors.Blue;
        Padding = 10;
    }
}

the view model that it is data-binded to:

public partial class MainViewModel : INotifyPropertyChanged
{
    public ObservableCollection<PswdWthGrp> PswdGrp { get; set; } = [];
    public List<PassWrds> PswrdData = [];
    public List<Groups> GroupData = [];

    public void PopPasswdWthGrpData()
    {
        var sets = ConnStr!.Table<LakSettings>().ToListAsync().Result;
        var crypto = sets[0].PassKey;

        if(PswdGrp.Count > 0)
            PswdGrp.Clear();

        // At this point the PswrdData and GroupData lists are already populated.

        foreach (var grp in GroupData)
        {
            var query = from p in PswrdData
                        where p.GrpId == grp.Id
                        select new PswdWthGrp
                        {
                            Id = p.Id,
                            Title = p.Title!,
                            SiteAddr = p.SiteAddr!,
                            UsrName = p.UsrName!,
                            Passwrd = Laks.Decrypt(p.Passwrd!, crypto!),
                            GrpName = grp.GrpName,
                            Notes = (!string.IsNullOrWhiteSpace(p.Notes) ? "X" : "")
                        };

            foreach (var item in query)
            {
                PswdGrp.Add(item);
            }
        }
    }

So after I ran the program, and checked the Title and SiteAddr propertied in the DataTemplate section of the datagrid I get the message:

Member not found in data context 'MainViewModel'

So my question is how do I make the column show data when I know there is data in the two fields that the class is using?

发布评论

评论列表(0)

  1. 暂无评论