当前位置: 动力学知识库 > 问答 > 编程问答 >

WPF OpacityMask moves with negative Margin of child element

问题描述:

When I place an element with a negative Margin into a ie. Border with an OpacityMask, the mask is moved as well.

Does anyone know if there is a way to apply an opacity mask to the viewport of the Border control regardless of any margins?

What I am trying to achieve is have only the center line of text of the Border's content should be fully visible. I move the Text (in my case an ItemsSource, actually) by setting the Margin of that element within the border to a negative value. This creates a cool effect that the other lines also loose in opacity and therefore the center line is plainly visible as being the current line. Unfortunately WPF also moves my Mask so as I move the content the fully visible line also moves upward within the Border control.

<Popup x:Name="PART_Popup"

VerticalOffset="{TemplateBinding TumblerVOffset}" IsOpen="{TemplateBinding IsTumblerVisible}"

PlacementTarget="{Binding RelativeSource={RelativeSource AncestorType={x:Type controls:TouchEditBox}}}"

MinWidth="{TemplateBinding TumblerWidth}" AllowsTransparency="True"

Height="{TemplateBinding TumblerHeight}" Placement="Relative">

<Border Background="{StaticResource TumblerBackground}" BorderBrush="White" BorderThickness="1">

<Border.OpacityMask>

<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">

<LinearGradientBrush.GradientStops>

<GradientStop Color="Transparent" Offset="0.0" />

<GradientStop Color="Black" Offset="0.4" />

<GradientStop Color="Black" Offset="0.6" />

<GradientStop Color="Transparent" Offset="1.0" />

</LinearGradientBrush.GradientStops>

</LinearGradientBrush>

</Border.OpacityMask>

<ItemsControl ItemsSource="{Binding TumblerHandler.Tumblers, RelativeSource={RelativeSource TemplatedParent}}"

HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}">

<ItemsControl.ItemsPanel>

<ItemsPanelTemplate>

<StackPanel Orientation="Horizontal" />

</ItemsPanelTemplate>

</ItemsControl.ItemsPanel>

</ItemsControl>

</Border>

</Popup>

网友答案:

I found a solution - set ClipToBounds="True" on the Container which holds the elements who's Margin is negative (in my case the ItemsControl).

分享给朋友:
您可能感兴趣的文章:
随机阅读: