silverLight:OpacityMask 不适用于自定义控件

发布于 2024-12-09 20:57:03 字数 2251 浏览 1 评论 0原文

我通过在覆盖层上应用 OpacityMask 将禁用的 ImageButton 自定义控件上的图像变灰。预期的效果是只有图像的主体变灰,因此图像上没有边框或框。

问题是:只要通过绑定 getter(在 {TemplateBinding XXX} 中编写的内容)访问图像 URI,掩码将不适用。但是,如果明确指定 URI,则掩码将起作用。那么问题来了,为什么TmplateBinding会出现这个问题以及如何解决。

定义自定义图像按钮的 XAML 代码为:

            <my1:ImageButton Height="24" Width="24" x:Name="imageButton1" IsEnabled="False" ImageSource="/SilverlightApplication4;component/Undo_24x24.png" Margin="178,75,198,201">
            <Image Source="/SilverlightApplication4;component/Undo_24x24.png" Height="24" Width="24" />
        </my1:ImageButton>

自定义属性处理程序代码为:

    public class ImageButton: Button
{
    public static readonly DependencyProperty ImageSourceProperty = DependencyProperty.Register("ImageSource", typeof(ImageSource), typeof(ImageButton), new PropertyMetadata(null));
    public ImageSource ImageSource
    {
        get
        {
            return (ImageSource)this.GetValue(ImageSourceProperty);
        }
        set
        {
            this.SetValue(ImageSourceProperty, value);
        }
    }
}

自定义控件的样式代码(禁用状态):

                            <Rectangle x:Name="DisabledVisualElement" Fill="Red" Opacity="0" >
                            <Rectangle.OpacityMask>
                                <ImageBrush ImageSource="{TemplateBinding ImageSource}"/>                                   
                            </Rectangle.OpacityMask>
                        </Rectangle>

有趣的是,以下代码片段放置在样式模板内

<Image Source="{TemplateBinding ImageSource}" Height="24" Width="24" />

,并且

                            <Rectangle x:Name="DisabledVisualElement" Fill="Red" Opacity="0" >
                            <Rectangle.OpacityMask>
                                <ImageBrush ImageSource="/SilverlightApplication4;component/Undo_24x24.png"/>                                   
                            </Rectangle.OpacityMask>
                        </Rectangle>

都可以正常工作。所以问题似乎在于 ImageSource="{TemplateBinding ImageSource}" 不知何故无法检索正确的 URI 或由于某些其他原因而无法工作。那么,如何纠正这个问题呢?

I'm graying out images on disabled ImageButton custom controls by applying OpacityMask on an Overlay. The intended effect is that only the body of an image is grayed out, so no border or box is laid over the image.

The problem is: As far as image URI is accessed through a bound getter (the thing written in {TemplateBinding XXX}) the mask would not apply. However if the URI is stated explicitly, the mask would work. So the question is, why the problem occurs in case of TmplateBinding and how to resolve it.

The XAML code defining the custom Image Button is:

            <my1:ImageButton Height="24" Width="24" x:Name="imageButton1" IsEnabled="False" ImageSource="/SilverlightApplication4;component/Undo_24x24.png" Margin="178,75,198,201">
            <Image Source="/SilverlightApplication4;component/Undo_24x24.png" Height="24" Width="24" />
        </my1:ImageButton>

The custom property handler code is:

    public class ImageButton: Button
{
    public static readonly DependencyProperty ImageSourceProperty = DependencyProperty.Register("ImageSource", typeof(ImageSource), typeof(ImageButton), new PropertyMetadata(null));
    public ImageSource ImageSource
    {
        get
        {
            return (ImageSource)this.GetValue(ImageSourceProperty);
        }
        set
        {
            this.SetValue(ImageSourceProperty, value);
        }
    }
}

The style code of the custom control (disabled state):

                            <Rectangle x:Name="DisabledVisualElement" Fill="Red" Opacity="0" >
                            <Rectangle.OpacityMask>
                                <ImageBrush ImageSource="{TemplateBinding ImageSource}"/>                                   
                            </Rectangle.OpacityMask>
                        </Rectangle>

interestingly is that both, the following code snippets placed inside the style template

<Image Source="{TemplateBinding ImageSource}" Height="24" Width="24" />

as well as

                            <Rectangle x:Name="DisabledVisualElement" Fill="Red" Opacity="0" >
                            <Rectangle.OpacityMask>
                                <ImageBrush ImageSource="/SilverlightApplication4;component/Undo_24x24.png"/>                                   
                            </Rectangle.OpacityMask>
                        </Rectangle>

both work correctly. So the problem seems to lie on that that ImageSource="{TemplateBinding ImageSource}" somehow does not retrieve the correct URI or does not work for some other reason. So, how to correct this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱冒险 2024-12-16 20:57:03

TemplateBinding 的功能不如常规绑定那么齐全。 TemplateBinding 无法自动将字符串/uri 转换为实际的位图。

解决方案(使用常规绑定):

<Rectangle x:Name="DisabledVisualElement" Fill="Red" Opacity="0" >
    <Rectangle.OpacityMask>
        <ImageBrush ImageSource="{Binding Path=ImageSource, RelativeSource={RelativeSource TemplatedParent}}"/>                                   
    </Rectangle.OpacityMask>
</Rectangle>

TemplateBinding isn't as fully featured as the regular binding. TemplateBinding is not able to automatically convert the string/uri into an actual bitmap.

Solution (use a regular binding):

<Rectangle x:Name="DisabledVisualElement" Fill="Red" Opacity="0" >
    <Rectangle.OpacityMask>
        <ImageBrush ImageSource="{Binding Path=ImageSource, RelativeSource={RelativeSource TemplatedParent}}"/>                                   
    </Rectangle.OpacityMask>
</Rectangle>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文