【问题标题】:Binding button text to variable in Carouselview将按钮文本绑定到 Carouselview 中的变量
【发布时间】:2018-07-05 00:49:22
【问题描述】:

我对 Xamarin 相当陌生,我正在尝试将文本数据绑定到我希望在轮播视图中存在的按钮。我无法让按钮显示或显示文本。当按钮位于轮播视图之外时,绑定有效,因为我可以使用 x:Name。我的研究告诉我,我不能在轮播中使用 x:Name,因为它期望控件发生变化。

按钮显示日期,当您向左或向右滑动时,日期会相应更改,我可以通过滑动更改日期,我只是看不到任何内容。

            <forms:CarouselView x:Name="MainCarouselView" Grid.Row="1" >
                <forms:CarouselView.ItemTemplate>
                    <DataTemplate>
                        <Button Text="{Binding buttonDate}" BackgroundColor="Red"/>
                    </DataTemplate>
                </forms:CarouselView.ItemTemplate>
            </forms:CarouselView>

代码如下。

public MyPage()
{           
   InitializeComponent();

   this.BindingContext = this;
   var buttonDate = new DateButton
   {
       ButtonDate = DateTime.Now.ToString()
   };
}

public class DateButton
{
    public string ButtonDate { get; set; }
}

提前感谢您的任何指点。

【问题讨论】:

    标签: c# xamarin xamarin.forms carousel


    【解决方案1】:
    public class Item {
      DateTime Date { get; set; }
    }
    
    var data = new List<Item>();
    
    data.Add(new Item { Date = DateTime.Now };
    data.Add(new Item { Date = DateTime.Now.AddDays(1) };
    data.Add(new Item { Date = DateTime.Now.AddDays(2) };
    
    MainCarouselView.ItemsSource = data;
    

    因为您的 ItemsSource 是 List&lt;Item&gt;,DataTemplate 的绑定上下文将是当前“活动”的 Item,因此 Binding Path 应该是 Item 上的 Property

    <Button Text="{Binding Date}" BackgroundColor="Red"/>
    

    【讨论】:

      猜你喜欢
      • 2018-09-03
      • 2020-03-12
      • 1970-01-01
      • 1970-01-01
      • 2017-07-30
      • 2011-12-10
      • 2012-09-06
      • 2016-09-09
      • 1970-01-01
      相关资源
      最近更新 更多