【问题标题】:Dynamic Clipping Width in Silverlight Within Canvas画布内 Silverlight 中的动态剪裁宽度
【发布时间】:2011-04-13 13:00:27
【问题描述】:

为什么这会引发错误,我该如何解决...我需要将剪切矩形设置为动态的。

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="42"/>
        <ColumnDefinition x:Name="ListBoxContainer" Width="*"/>
        <ColumnDefinition Width="42"/>
    </Grid.ColumnDefinitions>


    <Canvas>
        <Button x:Name="btnGalleryLeft" 
                Click="btnGalleryLeftClick" 
                Style="{StaticResource GalleryNavigationLeft}"
                Canvas.Left="7"
                Canvas.Top="50" />
    </Canvas>
    <Canvas Grid.Column="1" x:Name="ListboxWrapper">
        <Canvas.Clip>
            <RectangleGeometry>
                <RectangleGeometry.Rect>
                    <Rect X="0" Y="0" 
                          Width="{Binding ElementName=ListBoxContainer, Path=Width}"
                          Height="{Binding ElementName=ListBoxContainer, Path=Height}"/>
                </RectangleGeometry.Rect>
            </RectangleGeometry>
        </Canvas.Clip>
        <ListBox x:Name="ListBox1" 
                 Margin="15, 18, 15, 0"  
                 Style="{StaticResource GalleryListBoxStyle}" 
                 ItemsSource="{Binding DocItemCollection}" 
                 SelectionChanged="ListBox_SelectionChanged" 
                 MouseLeftButtonUp="ListBox_MouseLeftButtonUp" 
                 Canvas.Top="0"
                 Canvas.Left="0"
               />
    </Canvas>
    <Canvas Grid.Column="2">
        <Button x:Name="btnGalleryRight"                     
                Click="btnGalleryRightClick" 
                Style="{StaticResource GalleryNavigationRight}"
                Margin="0, 0, 7, 0"
                Canvas.Top="50" />         

【问题讨论】:

  • 错误是 xamlparseexception 错误,说 System.Windows.Rect.X 是只读属性,不能设置。那我该如何设置...?

标签: silverlight xaml clipping


【解决方案1】:

您仍然可以使用RectangleGeometry 作为剪切区域。只需订阅Loaded 事件,并在其中创建一个新的RectangleGeometry

void control_Loaded(object sender, RoutedEventArgs e)
        {
            LayoutRoot.DataContext = this;

            Rect rect = new Rect(0, 0, yourWidth, yourHeight);
            RectangleGeometry reo = new RectangleGeometry();
            reo.Rect = rect;
            this.canvas.Clip = reo;
        }

只是补充一点信息

此外,不透明度和剪辑属性设置由 组成线程。但是,如果不透明蒙版或非矩形 动画对象上使用剪辑,这些操作将被传递 到 UI 线程。这意味着即使剪辑区域是 矩形形状,但使用 PathGeometry 操作将是 传递给 UI 线程。因此,请务必始终使用 如果可能,使用 RectangleGeometry 剪切路径。

【讨论】:

    【解决方案2】:

    解决方案终于....经过大量的诅咒和咒骂。要是一切都像 CSS 中一样直接向前就好了(溢出血腥的隐藏属性)。

    前端:

    <Canvas Grid.Column="1" x:Name="ListboxWrapper" Background="Black">
                <ScrollViewer Background="Red" 
                              FlowDirection="LeftToRight" 
                              HorizontalScrollBarVisibility="Hidden" 
                              VerticalScrollBarVisibility="Hidden"
                              x:Name="ScrollViewerClipper">                
                    <Canvas x:Name="CarouselContainer">
                        <gallery:ImageCarousel x:Name="carousel" />
                    </Canvas>
                </ScrollViewer>
    

    后端:

    public GalleryPanel()
            {
                InitializeComponent();
    
                LayoutRoot.SizeChanged +=new SizeChangedEventHandler(LayoutRoot_SizeChanged);
            }
    
            private void LayoutRoot_SizeChanged(object s, SizeChangedEventArgs e)
            {
                ScrollViewerClipper.Width = ListboxWrapper.ActualWidth;
                ScrollViewerClipper.Height = ListboxWrapper.ActualHeight;
            }
    

    【讨论】:

    • (使用滚动查看器作为剪辑器而不是剪辑矩形)
    【解决方案3】:

    我认为你把事情复杂化了。绑定到 ColumnDefinition 的宽度/高度不起作用。我只需创建将订阅 SizeChanged 事件并根据 ActualWidth[Height] 设置裁剪的行为。

    【讨论】:

    • 我对 silverlight 很陌生.. 现在我该怎么做?我还在通过 SizeChange 事件设置裁剪矩形的宽度和高度吗?
    猜你喜欢
    • 1970-01-01
    • 2012-10-15
    • 1970-01-01
    • 2012-08-01
    • 1970-01-01
    • 2018-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多