【问题标题】:Xamarin: Binding Class Object property to ListView TextXamarin:将类对象属性绑定到 ListView 文本
【发布时间】:2018-04-10 23:58:58
【问题描述】:

我有一个对象列表List<Shift> ListOfShift = new List<Shift>();,我想在 Xamarin 中创建 ListView,在单元格中显示 Shift 对象的第一个值。

这是我的 Shift 类代码:

public String StringShift { get; set; }
DateTime StartOfShift { get; set; }
DateTime EndOfShift { get; set; }


    public Shift(DateTime StartShift, DateTime EndShift)
    {
        StartOfShift = StartShift;
        EndOfShift = EndShift;

        StringShift = Convert.ToString(StartOfShift);
    }

    public string StringShow()
    {
        string ShiftText = Convert.ToString(StartOfShift) + " " + Convert.ToString(EndOfShift);
        return ShiftText;
    }

这里是以下 XAML:

<ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <Label Text="{Binding .}" />
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>

我尝试了以下方法:

<Label Text="{Binding .}" />
<Label Text="{Binding Shift.StartOfShift}" /> // Prints blank
<Label Text="{Binding Shift.StringShift}" /> // Prints blank

如何使 Text 值成为预期的对象属性?

注意:

列表确实有效,并且正在添加具有正确值的项目,我能够按预期使用列表对象,我只想显示它们。

编辑:

完整的 XAML 代码

<Grid>

<Grid.ColumnDefinitions>
    <ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>       
<Grid.RowDefinitions>
    <RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button
    x:Name="ShiftAdd"
    Clicked="AddButton"
    Text="Add"
    Grid.Column="0"
    Grid.ColumnSpan="2"
    Grid.Row="2"
/>
<ListView 
    x:Name="ShiftListViewer"
    BackgroundColor="AntiqueWhite"
    SeparatorColor="Black"
    Grid.Column="0"
    Grid.ColumnSpan="4"
    Grid.Row="0"
    Grid.RowSpan="2">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
    <Label Text="{Binding Shift.ShiftOfStart}" />
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</ContentPage>

C#代码:

//Within a button event

DateTime Shift1 = new DateTime(2018,1,1,10,0,0);
DateTime Shift2 = new DateTime(2018,1,1,19,0,0);
New List<Shift> ListOfShifts = new List<Shift>();
ListOfShifts.Add(new Shift(Shift1, Shift2));

ShiftListViewer.ItemsSource = ListOfShifts;
    ShiftListViewer.BindingContext = "Shift.StartOfShift";

【问题讨论】:

  • @StacksSlacks 你是如何绑定你的 ListView 的? ListView 中的 ItemsSource 在哪里?
  • C# 代码:ShiftListViewer.ItemSource = ListOfShifts; * XAML: *
  • @StacksSlacks 你在使用 MVVM 吗?如果你看一下这里,这是同样的问题。如果没有看到该 xaml 文件中的所有代码,很难准确地说出。 forums.xamarin.com/discussion/67596/…
  • @SSharp 我已经添加了完整的 XAML
  • @SlackStacks 只是为了排除,如果你去掉 ShiftListViewer.BindingContext,然后将 Label Binding 设置为 StartOfShift,它会显示吗?我不熟悉以这种方式设置上下文。

标签: c# listview xamarin


【解决方案1】:

如果您的ItemsSourceList&lt;Shift&gt;,那么您的ListView 中的每个元素都是Shift 类型。这意味着绑定表达式将相对于Shift

// tries to display Shift object
<Label Text="{Binding .}" />
// won't work, there is no "Shift" property on the Shift object
<Label Text="{Binding Shift.StartOfShift}" /> 
// won't work, there is no "Shift" property on the Shift object
<Label Text="{Binding Shift.StringShift}" /> 

// this should work, "StringShift" is a public property of Shift
<Label Text="{Binding StringShift}" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-08
    • 2020-11-26
    • 2017-09-25
    • 2018-11-16
    • 1970-01-01
    • 2022-06-11
    • 2018-07-02
    • 2016-12-30
    相关资源
    最近更新 更多