Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF列表框以不同方式显示最后一项_Wpf_Xaml_Listbox_Datatemplate_Controltemplate - Fatal编程技术网

WPF列表框以不同方式显示最后一项

WPF列表框以不同方式显示最后一项,wpf,xaml,listbox,datatemplate,controltemplate,Wpf,Xaml,Listbox,Datatemplate,Controltemplate,我想要一个listbox,它允许用户从DB中提取20个项目,如果有更多的项目需要提取,则在listbox的最后一行显示一个提示。当用户单击最后一行时,应该从数据库中检索其他项目,直到没有更多项目,最后一行显示此信息 第一: listitem1 listitem2 ... listitem19 listitem20 Button: <get_more> listitem1 清单项目2 ... 清单项目19 清单项目20 按钮: 按下按钮后: listitem1 listitem2

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

第一:

listitem1
listitem2
...
listitem19
listitem20
Button: <get_more>
listitem1
清单项目2
...
清单项目19
清单项目20
按钮:
按下按钮后:

listitem1
listitem2
...
listitem39
listitem40
Info: <no more items>
listitem1
清单项目2
...
清单项目39
清单项目40
信息:
所有这些都只能在XAML中完成吗? 实现这一点的最佳解决方案是什么?

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

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

1/入门:A
DockPanel

<DockPanel LastChildFill="True">
   <Button DockPanel.Dock="Bottom" />
   <ListBox  />
</DockPanel>
在ViewModel中:

<ListBox ItemsSource="{Binding ListElements}" />
private ObservableCollection<String> _listElements;

        public ObservableCollection<String> ListElements
        {
            get { return _listElements; }
            set { _listElements = value; }
        }
public String ButtonString
{
   get 
   {
      //There, define if there are any more things to display
   }
}
private void GetMore()
{
   //append to the _listElements new elements from the list 
   //Update the ButtonString if there are no more elements
}
在ViewModel中:

<ListBox ItemsSource="{Binding ListElements}" />
private ObservableCollection<String> _listElements;

        public ObservableCollection<String> ListElements
        {
            get { return _listElements; }
            set { _listElements = value; }
        }
public String ButtonString
{
   get 
   {
      //There, define if there are any more things to display
   }
}
private void GetMore()
{
   //append to the _listElements new elements from the list 
   //Update the ButtonString if there are no more elements
}
4/您的
按钮
触发一个
命令
启动一个方法,比如说
GetMore()

就这样


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

谢谢@Damascus。您的解决方案肯定会起作用,但将按钮放在列表框外有一些缺点:它们都位于具有垂直scrollbar的视图中,在您的情况下,我认为这将是DockPanel的滚动条。我希望在列表框上进行滚动,例如,我可以使用滚轮直接在列表框中进行滚动。您可以仅为列表显示滚动条。这实际上就是将按钮从列表框中放在一边的目的。只需从dockPanel中删除滚动条(我不确定是否有
IsScrollable
属性,但至少可以将
Scrollviewer.VerticalScrollbarVisibility
设置为
Hidden
对于
DockPanel
,列表将因此处理该属性。问题是,我希望列表框和按钮处于有限的视图中(比如100像素)。listbox扩展到1000像素,然后是按钮。我将listbox和按钮都放在一个StackPanel中,该面板位于ScrollViewer中。这样做实际上是可以的,只是我无法使用鼠标滚轮滚动!似乎鼠标滚轮事件被listbox捕获(因为它被扩展了,所以没有任何作用)事件不会出现在scrollviewer上。也许我应该解决这个问题,而不是搜索其他解决方案:)我认为问题更多的是在你的布局上,而不是在流程本身上,你可以很容易地使用我给你的东西,只需在XAML中更改第一部分。我想说这是一个焦点问题,你是否尝试过显式地将焦点设置到你的scrollviewer上?