silverLight:OpacityMask赢得';不适用于自定义控件
我通过在覆盖上应用OpacityMask,使禁用的ImageButton自定义控件上的图像变灰。预期的效果是,只有图像的主体变灰,因此图像上没有边框或方框 问题是:只要通过绑定getter(在{TemplateBinding XXX}中编写的东西)访问图像URI,掩码就不会应用。但是,如果URI是显式声明的,则掩码将起作用。所以问题是,为什么TmplateBinding会出现问题,以及如何解决它 定义自定义图像按钮的XAML代码为:silverLight:OpacityMask赢得';不适用于自定义控件,silverlight,controls,imagebutton,opacitymask,Silverlight,Controls,Imagebutton,Opacitymask,我通过在覆盖上应用OpacityMask,使禁用的ImageButton自定义控件上的图像变灰。预期的效果是,只有图像的主体变灰,因此图像上没有边框或方框 问题是:只要通过绑定getter(在{TemplateBinding XXX}中编写的东西)访问图像URI,掩码就不会应用。但是,如果URI是显式声明的,则掩码将起作用。所以问题是,为什么TmplateBinding会出现问题,以及如何解决它 定义自定义图像按钮的XAML代码为: <my1:ImageButton
<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>
自定义控件的样式代码(禁用状态):
有趣的是,以下两个代码片段都放在样式模板中
<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或由于其他原因无法工作。那么,如何纠正这个问题呢?TemplateBinding的功能不如常规绑定充分。TemplateBinding无法自动将字符串/uri转换为实际位图 解决方案(使用常规绑定):
<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>
<Rectangle x:Name="DisabledVisualElement" Fill="Red" Opacity="0" >
<Rectangle.OpacityMask>
<ImageBrush ImageSource="{Binding Path=ImageSource, RelativeSource={RelativeSource TemplatedParent}}"/>
</Rectangle.OpacityMask>
</Rectangle>