WPF ListBox 以不同方式显示最后一项

标签 wpf xaml listbox datatemplate controltemplate

我想要一个列表框,它允许用户从数据库中获取比方说 20 个项目,并在列表框的最后一行显示提示(如果有更多项目需要获取)。当用户单击最后一行时,应从数据库中检索其他项目,直到没有更多项目并且最后一行显示此信息。

第一:

listitem1
listitem2
...
listitem19
listitem20
Button: <get_more>

按下按钮后:

listitem1
listitem2
...
listitem39
listitem40
Info: <no more items>

所有这些都可以仅在 XAML 中完成吗? 实现这一点的最佳解决方案是什么?

最佳答案

伙计 - 一切都可以用 XAML 完成:D

按照 MVVM 方法,我建议您执行以下操作:

1/入门:DockPanel

<DockPanel LastChildFill="True">
   <Button DockPanel.Dock="Bottom" />
   <ListBox  />
</DockPanel>

2/将您的 ListBox 绑定(bind)到 ViewModel 中的 ObservableCollection:

<ListBox ItemsSource="{Binding ListElements}" />

在 View 模型中:

private ObservableCollection<String> _listElements;

        public ObservableCollection<String> ListElements
        {
            get { return _listElements; }
            set { _listElements = value; }
        }

3/将 Button 的内容绑定(bind)到预定义的 String:

<Button Content="{Binding ButtonString}" />

在 View 模型中:

public String ButtonString
{
   get 
   {
      //There, define if there are any more things to display
   }
}

4/您的 Button 触发 Command 启动一个方法,比方说 GetMore() :

<Button Content="{Binding ButtonString}" Command="{Binding GetMoreCommand} />

在 View 模型中:

private void GetMore()
{
   //append to the _listElements new elements from the list 
   //Update the ButtonString if there are no more elements
}

就这样吧!

(如果需要,您还可以定义一个按钮,例如从 ObservableCollection 中删除内容)

关于WPF ListBox 以不同方式显示最后一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5832947/

相关文章:

WPF : Chainging another control's property in some control's Trigger

c# - WPF 中的源绑定(bind),如 Asp.Net

c# - ListBoxItem 与 ListViewItem 的 GridViewColumn 绑定(bind)

wpf - WPF DataGrid 的 GroupStyle 上的 HeaderTemplate 和 ContainerStyle 有什么区别?

c# - 在 MVVM 应用程序中使用 RSS 提要加载 Xdocument

c# - 在 WPF 中绘制数千个矩形

c# - 单击按钮后更改按钮的效果 WPF

WPF/XAML - 允许 ComboBox 具有不在 ItemsSource 中的 SelectedValue

c# - 以编程方式选择 ListBox 中的多个项目?

c# - WPF:列表框显示成员