简体   繁体   English

WPF不透明蒙版固定位置

[英]WPF Opacity Mask at fixed location

I need to get rid of a part of a component ( make it totally transparent ) , a small bar at the bottom of it actually. 我需要去除组件的一部分(使其完全透明),实际上是它底部的一个小条。 It's a fixed size something like 25-30px but the problem is this component will be resized a lot so i can't just put an image as opacity mask. 它的大小是固定的,例如25-30px,但问题是此组件将被调整很多尺寸,因此我不能只将图像用作不透明蒙版。 ( which will look bad at different sizes ) Even if the component is 300x300 or 1000x200 , i need to get bottom 25px disappear somehow. (在不同的尺寸下看起来不好)即使该组件是300x300或1000x200,我也需要以某种方式使底部25px消失。

I searched about opacity mask&drawing brush but no luck , can't find a way to dock it at the bottom of component. 我搜索了不透明蒙版和绘图笔,但是没有运气,找不到将其停靠在组件底部的方法。

By the way , not sure if it matters but the component I'm talking about is WPFChromium browser control. 顺便说一下,不确定是否重要,但是我要谈论的组件是WPFChromium浏览器控件。

Is there a way to achieve this by opacity mask or something like viewbox etc? 有没有一种方法可以通过不透明蒙版或诸如viewbox等来实现?

Thanks in advance! 提前致谢!

You could use a LinearGradientBrush as OpacityMask for the Control and Bind the Offset to the ActualHeight of the Control and then subtract 25 from the value and divide it by the ActualHeight in order to get the value in %. 您可以将LinearGradientBrush用作控件的OpacityMask,并将偏移量绑定到控件的ActualHeight,然后从该值中减去25,然后将其除以ActualHeight,以便以%的形式获得该值。 This should give you a transparent part of 25px at the Bottom 这应该在底部为您提供25px的透明部分

<WebBrowser Name="webBrowser">
    <WebBrowser.OpacityMask>
        <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
            <GradientStop Color="#FFFF0000"
                          Offset="{Binding ElementName=webBrowser,
                                           Path=ActualHeight,
                                           Converter={StaticResource OffsetConverter},
                                           ConverterParameter=25}"/>
            <GradientStop Color="#00000000"
                          Offset="{Binding ElementName=webBrowser,
                                           Path=ActualHeight,
                                           Converter={StaticResource OffsetConverter},
                                           ConverterParameter=25}"/>
        </LinearGradientBrush>
    </WebBrowser.OpacityMask>
</WebBrowser>

The OffsetConverter OffsetConverter

public class OffsetConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        double height = (double)value;
        double subract = System.Convert.ToDouble(parameter.ToString());
        double opacityMaskHeight = height - subract;
        return opacityMaskHeight / height;
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM