C# WPF ItemsSource绑定仅显示列表的最后一项
如何在itemssource绑定中仅显示列表的最后一项 下面是我当前的代码 xamlC# WPF ItemsSource绑定仅显示列表的最后一项,c#,wpf,list,itemssource,C#,Wpf,List,Itemssource,如何在itemssource绑定中仅显示列表的最后一项 下面是我当前的代码 xaml 如果要绑定到列表中的特定项,可以创建一个要绑定的公共变量。使用您提供的内容,我创建了一个适用于列表中最后一项的示例,我所做的只是创建一个名为LastItem的新变量,并更改了绑定在项目中的方式。这只是众多方法中的一种 xaml <StackPanel Orientation="Vertical" HorizontalAlignment="Center" Width="800"> <Text
如果要绑定到列表中的特定项,可以创建一个要绑定的公共变量。使用您提供的内容,我创建了一个适用于列表中最后一项的示例,我所做的只是创建一个名为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>
这将提供以下输出:
如果只想显示最后一个,为什么要使用items控件。如果确实需要UpgradeTicketStorage列表,为什么不绑定到
UpgradeTicketStorage.Last()的表达式属性
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)"});
<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(); }
}