WPF ItemsSource绑定仅显示列表的最后一项

时间:2018-08-23 06:25:32

标签: c# wpf list itemssource

如何在itemssource绑定中仅显示列表的最后一项?

下面是我当前的代码。

xaml

<ItemsControl ItemsSource="{Binding UpgradeTicketStorage}" VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Recycling" >
   <ItemsControl.ItemTemplate>
        <DataTemplate>
             <StackPanel Orientation="Vertical" HorizontalAlignment="Center" Width="800">
                <TextBlock  Text="{Binding TicketTitle}"  Style="{StaticResource TicketSelectionSubTitle}" TextAlignment="Left" />
                <TextBlock  Text="{Binding TicketDescription}"  TextWrapping="Wrap" Style="{StaticResource TicketSelectionSubTitle2}" FontSize="19" TextAlignment="Left"/>
             </StackPanel>
       </DataTemplate>
   </ItemsControl.ItemTemplate>
</ItemsControl>

从类绑定中,我确实添加了2条记录,分别为ticketAticketB,我如何只显示ticketB信息?而不是A和B

课程

public class UpgradeTicketDescription : ViewModelBase
    {

        public string TicketTitle { get; set; }
        public string TicketDescription { get; set; }
    }

List<UpgradeTicketDescription> _UpgradeTicketStorage;
public List<UpgradeTicketDescription> UpgradeTicketStorage
    {
        get { return _UpgradeTicketStorage; }
        set { _UpgradeTicketStorage = value; OnPropertyChanged("UpgradeTicketStorage"); }
    }
 UpgradeTicketStorage.Add(new UpgradeTicketDescription { TicketTitle = "TicketA", TicketDescription = "Observation DeckA (Single Ticket)"});
 UpgradeTicketStorage.Add(new UpgradeTicketDescription { TicketTitle = "TicketB", TicketDescription = "Observation DeckB (Single Ticket)"});

1 个答案:

答案 0 :(得分:0)

如果要绑定到列表中的特定项目,可以通过创建要绑定到的公共变量来进行处理。使用您提供的内容,我创建了一个适用于列表中最后一项的示例,我所做的就是创建一个名为LastItem的新变量,并更改了绑定在项目中的方式。这只是解决此问题的众多方法之一。

xaml

<StackPanel Orientation="Vertical" HorizontalAlignment="Center" Width="800">
  <TextBlock  Text="{Binding LastItem.TicketTitle}" TextAlignment="Left" />
  <TextBlock  Text="{Binding LastItem.TicketDescription}"  TextWrapping="Wrap" FontSize="19" TextAlignment="Left"/>
</StackPanel>

课程

public UpgradeTicketDescription LastItem
  {
  get { return UpgradeTicketStorage.Last(); }
  }

这提供了以下输出: enter image description here