多德 - 一切可以用XAML来完成:d
继MVVM的做法,我建议你做以下几点:
1 /入门:一个DockPanel
<DockPanel LastChildFill="True">
<Button DockPanel.Dock="Bottom" />
<ListBox />
</DockPanel>
2 /绑定你ListBox
到您的视图模型的ObservableCollection
:
<ListBox ItemsSource="{Binding ListElements}" />
在视图模型:
private ObservableCollection<String> _listElements;
public ObservableCollection<String> ListElements
{
get { return _listElements; }
set { _listElements = value; }
}
3 /绑定你Button
的内容预定义的String
:
<Button Content="{Binding ButtonString}" />
在视图模型:
public String ButtonString
{
get
{
//There, define if there are any more things to display
}
}
4 /你Button
启动一个Command
启动一个方法,让我们来看看AY GetMore()
:
<Button Content="{Binding ButtonString}" Command="{Binding GetMoreCommand} />
在视图模型:
private void GetMore()
{
//append to the _listElements new elements from the list
//Update the ButtonString if there are no more elements
}
而且你去那里!
(你也可以,如果需要的话,定义一个按钮从ObservableCollection
例如去除的东西)
感谢@Damascus。你的解决方案肯定会工作,但在列表框外面有按钮有一些缺点:它们都在一个垂直scroollbar的视图中,在你的情况下,我认为它会是DockPanel的滚动条。我希望滚动在ListBox上完成,以便例如我可以使用滚轮直接在列表框中滚动。 – mcanti 2011-05-02 05:05:20
您可以为列表显示滚动条。这实际上是将按钮放在ListBox旁边的目的。只需从dockPanel中移除滚动条(我不确定是否存在'IsScrollable'属性,但至少可以将'Scrollviewer.VerticalScrollbarVisibility'设置为'Hidden'来指定'DockPanel',因此列表将处理它 – Damascus 2011-05-02 07:04:48
问题是,我想让列表框和按钮在有限的视图中(让我们说100个像素)。列表框扩展到可以说1000个像素,然后出现按钮。我将列表框和按钮放入StackPanel中,放置在ScrollViewer中。这样做确实是可以的,但我无法使用鼠标滚轮进行滚动!看起来,鼠标滚轮事件被列表框捕获(它没有做任何事情,因为它被展开),事件不会向上滚动到滚动查看器。也许我应该解决这个问题,而不是寻找其他解决方案:) – mcanti 2011-05-03 08:01:39