【问题标题】:WPF ListBox display last item differentlyWPF ListBox 以不同方式显示最后一项
【发布时间】:2011-04-29 13:52:51
【问题描述】:

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

第一:

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

按下按钮后:

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

这一切都只能在 XAML 中完成吗? 实现这一点的最佳解决方案是什么?

【问题讨论】:

    标签: wpf xaml listbox datatemplate controltemplate


    【解决方案1】:

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

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

    1/ 入门:A DockPanel

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

    2/ 将 ListBox 绑定到 ViewModel 中的 ObservableCollection

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

    在 ViewModel 中:

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

    3/ 将Button 的内容绑定到预定义的String

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

    在 ViewModel 中:

    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} />
    

    在 ViewModel 中:

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

    你去吧!

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

    【讨论】:

    • 谢谢@Damascus。您的解决方案肯定会起作用,但是将 Button 放在列表框之外有一些缺点:它们都位于具有垂直滚动条的视图中,在您的情况下,我认为这将是 DockPanel 的滚动条。我希望在 ListBox 上进行滚动,例如,我可以使用滚轮直接在列表框中进行滚动。
    • 您可以只为列表显示滚动条。这实际上就是将按钮放在 ListBox 之外的目的。只需从您的dockPanel中删除滚动条(我不确定是否有IsScrollable属性,但至少您可以将Scrollviewer.VerticalScrollbarVisibility设置为HiddenDockPanel,因此列表将处理它跨度>
    • 问题是我想让列表框和按钮处于有限的视图中(比如说 100 像素)。列表框扩展到让我们说 1000 像素,然后是按钮。我将列表框和按钮都放入 StackPanel 中,该 StackPanel 位于 ScrollViewer 中。这样做实际上是可以的,除了我无法使用鼠标滚轮滚动!似乎鼠标滚轮事件被列表框捕获(它什么都不做,因为它已展开)并且该事件不会冒泡到滚动查看器。也许我应该解决这个问题,而不是寻找其他解决方案:)
    • 我认为问题在于您的布局而不是流程本身,您可以轻松使用我给您的内容,只需在 XAML 中更改第一部分。我会说这是一个焦点问题,您是否尝试过将焦点明确设置为滚动查看器?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多