滚动条是不可见的改变帆布内的控件位置后, [英] ScrollBars are not visible after changing positions of controls inside a Canvas

查看:183
本文介绍了滚动条是不可见的改变帆布内的控件位置后,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义画布控件WPF 画布继承。我使用它像这样在主窗口 -

I created a custom canvas control inheriting from WPF Canvas. I am using it like this in main window -

<ScrollViewer
    HorizontalScrollBarVisibility="Auto"
    VerticalScrollBarVisibility="Auto">
    <RTD:RTDesignerCanvas
        Margin="5"
        Background="White"
        x:Name="canvas1"
        Focusable="True"
        AllowDrop="True">
    </RTD:RTDesignerCanvas>
</ScrollViewer>

一切工作正常,但当我尝试设置控件的位置,里面像这样

Everything works fine but when I try to set the position of controls inside it like this

Canvas.SetTop(项,200);

滚动条是不可见的控制潜伏下来的地方。有趣的是,如果我添加另一个控制它滚动条是可见的,我可以向下滚动看到previous控制。

scrollbars are not visible and control is hidden down somewhere. Interestingly, if I add another control to it scroll bars are visible and I can scroll downwards to see the previous control.

我试图用

base.InvalidateVisual();
base.UpdateLayout();
base.InvalidateArrange();

变化性损益后的,但什么也没发生;我缺少的东西,或出现这种情况是由于一些bug?

after changing items Top or Left but nothing happens; Am I missing something or this happens due to some bug?

更新

澄清,说我有有一个帆布的宽度高度 100,100。现在,如果我移动使用的控制(在画布上已添加) Canvas.SetLeft(myControl,200)则它会移动到一个位置,这是不可见的默认和滚动条也被禁止,所以没有办法看到控制。

to clarify, say I have a canvas having its width, height as 100, 100. Now if I move a control(already added in canvas) using Canvas.SetLeft(myControl, 200) then it will move to a position which is not visible by default and scroll bars are also disabled, so there is no way to see that control.

现在,如果我添加其他控件来帆布,滚动条显示正确,我可以通过滚动看到previous控制。

Now if I add another control to Canvas, ScrollBars appear correctly and I can see the previous control by scrolling.

推荐答案

你有没有覆盖的的MeasureOverride 您的自定义画布?帆布将始终报告的DesiredSize(0,0),这样的ScrollViewer绝不会认为它需要滚动。

Did you override MeasureOverride in your custom Canvas? Canvas will always report a DesiredSize of (0, 0), so the ScrollViewer will never think it needs to scroll.

请参阅<一href="http://stackoverflow.com/questions/855334/wpf-how-to-make-canvas-auto-resize/863108#863108">this StackOverflow的答案这表明使用网格而不是画布,用定位Margin属性。网格将报告其大小基于所述尺寸和其孩子的位置,将这样的ScrollViewer知道它需要滚动。

See this StackOverflow answer which suggests using a Grid instead of a Canvas and using the Margin property for positioning. The Grid will report its size based on the size and position of its children, so the ScrollViewer will know it needs to scroll.

更新:

的ScrollViewer将给予孩子要求太多的大小,因为它要求,并且只需要滚动如果孩子比ScrollViewer的大。为了让它正确地滚动,你需要报告DesiredSize足够大以包含所有子控件。你可以做到这一点通过重写的MeasureOverride是这样的:

ScrollViewer will give its child ask much size as it asks for, and will only need to scroll if the child is larger than the ScrollViewer. In order to have it scroll properly, you'll need to report a DesiredSize that is large enough to include all of your child controls. You can do that by overriding MeasureOverride like this:

protected override Size MeasureOverride(Size constraint)
{
    base.MeasureOverride(constraint);
    var desiredSize = new Size();
    foreach (UIElement child in Children)
    {
        desiredSize = new Size(
            Math.Max(desiredSize.Width, GetLeft(child) + child.DesiredSize.Width),
            Math.Max(desiredSize.Height, GetTop(child) + child.DesiredSize.Height));
    }
    return desiredSize;
}

这是简单的解决方案,然而,就是要把一个事实,即网格类就已经测量这样的优势。您可以使用子元素的Margin属性来定位他们,而不是完全的Canvas.Left和Canvas.Top属性。如果你正在做

An easier solution, however, is to take advantage of the fact that the Grid class will already measure like this. You can use the Margin property of the child elements to position them exactly instead of the Canvas.Left and Canvas.Top properties. If you are currently doing

Canvas.SetLeft(item, 100);
Canvas.SetTop(item, 200);

在画布中的项目,可以转而做

for an item in the Canvas, you could instead do

item.Margin = new Thickness(100, 200, 0, 0);

要它在同一个地方内的一个单元的网格位置。

to position it in the same place within a one-cell Grid.

这篇关于滚动条是不可见的改变帆布内的控件位置后,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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