2

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

    <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" />         
4

3 回答 3

2

您仍然可以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 来剪切路径。

于 2012-12-13T04:20:52.433 回答
1

最终解决方案....经过多次诅咒和咒骂。要是一切都像 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;
        }
于 2010-09-17T07:08:46.683 回答
0

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

于 2010-09-17T06:32:25.490 回答