我试图弄清楚如何将文本添加到列表框的底部并显示它。在带有代码的 WPF 中,我会抓住 ScrollViewer 并对其进行操作,但我不知道如何使用 Caliburn 来完成它......
问问题
177 次
1 回答
2
你有几个选择。
1) 在您的 ViewModel 中,您可以调用GetView
并将其转换为您的视图类型并获取对 ScrollViewer 的引用。就像是:
var myView = this.GetView() as MyView;
var myScrollView = myView.MyScrollView;
这可以正常工作,但如果您试图不将视图耦合到视图模型,则并不理想。
选项 2) 是实现 IResult,请参阅此处的文档。
public class ScrollViewResult : IResult
{
public event EventHandler<ResultCompletionEventArgs> Completed = delegate { };
private ScrollViewResult ()
{
}
public void Execute (ActionExecutionContext context)
{
var view = context.View as FrameworkElement;
var scrollViewer = FindVisualChild<ScrollViewer>(view);
//do stuff to scrollViewer here
Completed (this, new ResultCompletionEventArgs { });
}
private static TChildItem FindVisualChild<TChildItem> (DependencyObject obj)
where TChildItem : DependencyObject
{
for (var i = 0; i < VisualTreeHelper.GetChildrenCount (obj); i++)
{
var child = VisualTreeHelper.GetChild (obj, i);
if (child != null && child is TChildItem)
return (TChildItem)child;
var childOfChild = FindVisualChild<TChildItem> (child);
if (childOfChild != null)
return childOfChild;
}
return null;
}
//this isn't required of course but comes in handy for
//having a static method and passing parameters to the
//ctor of the IResult
public static IResult DoSomething ()
{
return new ScrollViewResult ();
}
然后你可以这样称呼它:
public IEnumerable<IResult> SomeAction()
{
yield return ScrollViewResult.DoSomething();
}
于 2012-11-04T22:30:36.657 回答