2011-01-31 198 views
13

我需要浮出ListBox中的某些內容,如DataTemplateListBox.ItemTemplate的規定。我正在使用RenderTransform,但內容會被限制在ListBox的邊界上。對於整個視覺樹,ClipToBoundsFalseWPF裁剪即使不需要裁剪 - 如何關閉它?

我已經讀過WPF內部執行一些裁剪的地方,即使沒有用專用裁剪屬性指定。我也發現使用Canvas有時可以解決剪切問題,但它在這裏沒有幫助。

我該如何克服這個問題?這裏是我想修復的一些XAML。請注意矩形的整個左邊部分缺失。

<ListBox> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <Rectangle Fill="Red" Stroke="Green" StrokeThickness="4" Width="100" Height="50"> 
        <Rectangle.RenderTransform> 
         <TranslateTransform X="-50" /> 
        </Rectangle.RenderTransform> 
       </Rectangle> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 

     42 
    </ListBox> 

回答

19

ListBoxItem的越來越由ScrollViewerListBox模板裁剪。要解決這一點,我認爲你需要刪除從模板ScrollViewer,如果你需要滾動,你可以在一個ScrollViewer

<ScrollViewer HorizontalScrollBarVisibility="Auto" 
       VerticalScrollBarVisibility="Auto"> 
    <ListBox Margin="100,10,0,0"> 
     <ListBox.Template> 
      <ControlTemplate TargetType="{x:Type ListBox}"> 
       <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="1" SnapsToDevicePixels="true"> 
        <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
       </Border> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsEnabled" Value="false"> 
         <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </ListBox.Template> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <Rectangle Fill="Red" Stroke="Green" StrokeThickness="4" Width="100" Height="50"> 
        <Rectangle.RenderTransform> 
         <TranslateTransform X="-50" /> 
        </Rectangle.RenderTransform> 
       </Rectangle> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 42 
    </ListBox> 
</ScrollViewer> 

更新

ScrollViewer在模板包裹ListBox會生成ScrollContentPresenter其又具有以下GetLayoutClip

protected override Geometry GetLayoutClip(Size layoutSlotSize) 
{ 
    return new RectangleGeometry(new Rect(base.RenderSize)); 
} 

這CLA ss是密封的,所以你不能從它派生來覆蓋這個方法。你將不得不實現自己的ScrollContentPresenter(如MyScrollContentPresenter),可能你自己ScrollViewer使用MyScrollContentPresenter以及使這項工作(如果你在這個方法返回null我認爲ListBox的邊界下面的一些項目可能會變得可見好吧)

+0

你似乎發現了!但我需要`ScrollViewer`在原來的位置。你知道ScrollVeiwer的內部實現中有什麼特別的東西傷害了我嗎?這是可以在派生類中重寫的東西嗎?我曾經在某處讀過`GetLayoutClip`是罪魁禍首,但我無法讓它按我的方式工作。 – wpfwannabe 2011-02-01 07:09:39

+1

@wpfwannabe:更新了我的答案。你說得對,`GetLayoutClip`就是問題所在。不幸的是,這次它在一個Sealed類(`ScrollContentPresenter`)中,所以你不能從它派生出來。據我所知,這需要很多工作才能實現這個目標 – 2011-02-01 10:20:55

-2

我偶然發現瞭解決這個問題的方法,並且在解決這個問題時偶然發現了這個問題。如果在樣式模板中將ScrollViewer的Horizo​​ntalScrollMode和VerticalScrollMode更改爲「Disabled」,它將分別停止在各個方向上的裁剪。

編輯:可能不適用於WPF。我使用UWP應用進行測試。有問題的領域是:

ScrollViewer.Horizo​​ntalScrollMode = 「已禁用」

ScrollViewer.VerticalScrollMode = 「已禁用」