Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.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
C# WPF-确定哪个控件在ScrollViewer的画布中可见_C#_Wpf_Window_Visibility_Scrollviewer - Fatal编程技术网

C# WPF-确定哪个控件在ScrollViewer的画布中可见

C# WPF-确定哪个控件在ScrollViewer的画布中可见,c#,wpf,window,visibility,scrollviewer,C#,Wpf,Window,Visibility,Scrollviewer,我在xaml页面中有类似的内容: <ScrollViewer x:Name="PreviewvideosScrollViewer" HorizontalScrollBarVisibility="Auto" Width="1366" Height="480" VerticalScrollBarVisibility="Disabled"> <Canvas x:Name="VideoCanvas" HorizontalAlignment="Left">

我在xaml页面中有类似的内容:

<ScrollViewer x:Name="PreviewvideosScrollViewer" HorizontalScrollBarVisibility="Auto" Width="1366" Height="480" VerticalScrollBarVisibility="Disabled">
            <Canvas x:Name="VideoCanvas" HorizontalAlignment="Left">
            </Canvas>
</ScrollViewer>
或者这个:

private bool IsUserVisible(FrameworkElement element, FrameworkElement container)
    {
        if (!element.IsVisible)
            return false;

        var bounds = element.TransformToAncestor(container).TransformBounds(new Rect(0.0, 0.0, element.ActualWidth, element.ActualHeight));
        var rect = new Rect(0.0, 0.0, container.ActualWidth, container.ActualHeight);
        return rect.Contains(bounds.TopLeft) || rect.Contains(bounds.BottomRight);
    }

但两者都不起作用。问题出在哪里?在scrollviewer中使用画布?还是别的什么?谢谢

好吧,假设您在画布中拥有控制权

您希望获得相对于ScrollViewer的坐标,然后检查坐标是否介于0和ScrollViewer.ViewportWidth和ScrollViewer.ViewportHeight之间

如何将Canvas Cordinate转换为ScrollViewer Cordinate

首先,我们不考虑滚动,假设画布比ScrollViewer小,其水平对齐设置为左侧,垂直对齐设置为顶部

要获得相对于ScrollViewer的控件Cordinate,只需使用以下命令:

var x = Canvas.GetLeft(YourControl);

var y = Canvas.GetTop(YourControl);
现在假设我们也应用了滚动

var x = Canvas.GetLeft(YourControl) - ScrollViewer.HorizontalOffset;

var y = Canvas.GetTop(YourControl) - ScrollViewer.VerticalOffset;

if(x >= 0 && x <= ScrollViewer.ViewportWidth && y <= ScrollViewer.ViewportHeight)
  return true; // YAY
var x=Canvas.GetLeft(您的控件)-ScrollViewer.HorizontalOffset;
var y=Canvas.GetTop(YourControl)-ScrollViewer.VerticalOffset;

如果(x>=0&&x您使用画布的原因是什么?不同的布局容器是否适合您的需要?您是否检查了元素的边界是否正确,因为错误报告或使用的大小/定位值可能会导致交叉点检查失败。我在想这会有多困难。使用偏移量和viewpor这很简单也很清楚。谢谢,你的情况很简单,但我在winRT中做了类似的事情。在那里我有控件的缩放(RenderTransform),还有ScrollViewer上的缩放。加上控件的旋转。画布在ScrollViewer上居中,所以需要额外的计算。很快它就变得超级混乱:P。
var x = Canvas.GetLeft(YourControl) - ScrollViewer.HorizontalOffset;

var y = Canvas.GetTop(YourControl) - ScrollViewer.VerticalOffset;

if(x >= 0 && x <= ScrollViewer.ViewportWidth && y <= ScrollViewer.ViewportHeight)
  return true; // YAY