如何以编程方式在滚动查看器中添加网格 [英] How to add a grid inside a Scroll viewer programmatically

查看:127
本文介绍了如何以编程方式在滚动查看器中添加网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的XAML看起来像这样

 < navigation:Page x:Class =SilverlightApplication1.Home> 

< Grid x:Name =LayoutRoot>
<! -
< ScrollViewer>
<网格>
< TextBlock Text =myTextBlock/>
< / Grid>
< / ScrollViewer>
- >
< / Grid>



我想以编程方式



我的代码背后看起来像这样

  public partial class Home:Page 
{
public Home()
{
InitializeComponent();

ScrollViewer sv = new ScrollViewer();
Grid grid = new Grid();
TextBlock block = new TextBlock();

block.Text =我的文本块;
grid.Children.Add(block);

sv.ScrollIntoView(grid);
LayoutRoot.Children.Add(sv);




$ p
$ b

这不起作用,因为它只显示滚动查看器,但是文本块是隐藏的。



我缺少什么?



有没有办法添加孩子使用Silverlight工具包中提供的扩展方法ScrollIntoView以编程方式无需ScrollViewer控件?我没有为ScrollViewer元素找到一个名为Children的属性

感谢您的帮助

解决方案

您没有指定 ScrollViewer 的内容,只需在最后一行之前执行此操作即可。也可以删除 ScrollIntoView 方法。

  sv.Content = grid ; 

希望它有帮助。 :)

My XAML looks like this

<navigation:Page x:Class="SilverlightApplication1.Home">

    <Grid x:Name="LayoutRoot">
    <!--
    <ScrollViewer>
        <Grid>
            <TextBlock Text="myTextBlock" />
        </Grid>
    </ScrollViewer>
    -->
</Grid>

I want to programmatically do the commented part above via code behind.

And my code behind looks like this

public partial class Home : Page
{
    public Home()
    {
        InitializeComponent();

        ScrollViewer sv = new ScrollViewer();
        Grid grid = new Grid();
        TextBlock block = new TextBlock();

        block.Text = "My Text block";
        grid.Children.Add(block);

        sv.ScrollIntoView(grid);
        LayoutRoot.Children.Add(sv);

    }

This doesnt work, since it only shows the scroll viewer but the text block is hidden.

What is that I'm missing?

Is there a way to add children to the "ScrollViewer" control programmatically w/o using the extension method "ScrollIntoView" available in the silverlight toolkit? i didnt find a property called "Children" for ScrollViewer element

Thanks for the help

解决方案

You didn't specify the ScrollViewer's content, just do this before the last line. Also you can remove the ScrollIntoView method.

sv.Content = grid;

Hope it helps. :)

这篇关于如何以编程方式在滚动查看器中添加网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆