简体   繁体   English

带有OpacityMask的WPF DataTrigger

[英]WPF DataTrigger with OpacityMask

i´m using Mahapps.Metro and want to change an icon depending on an Property. 我正在使用Mahapps.Metro并希望根据属性更改图标。

Here is the Mahapps sample for their Icons 这是Mahapps的图标示例

Setting only the Backgroundcolor("Fill") of the rectangle everything works fine. 只设置矩形的Backgroundcolor(“Fill”)一切正常。 As soon as the OpacityMask is set the complete rectangle stays blank. 设置OpacityMask后,完整的矩形将保持空白。

<Rectangle Width="20" Height="20">
    <Rectangle.Style>
        <Style TargetType="{x:Type Rectangle}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=IsOnline}" Value="True">
                    <Setter Property="Fill" Value="Green" />
                    <Setter Property="OpacityMask">
                        <Setter.Value>
                            <VisualBrush Stretch="Fill" Visual="{StaticResource appbar_disconnect}" />
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=IsOnline}" Value="False">
                    <Setter Property="Fill" Value="Red" />
                    <Setter Property="OpacityMask">
                        <Setter.Value>
                            <VisualBrush Stretch="Fill" Visual="{StaticResource appbar_connect}" />
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Rectangle.Style>
</Rectangle>

Any ideas what i do wrong? 我有什么想法吗?

After long time i found a working solution for me: 很长一段时间后,我找到了一个有效的解决方案:

  • The first problem was the missing Ressource BlackBrush which is used inside the Icons.xaml 第一个问题是缺少Ressource BlackBrush,它在Icons.xaml中使用

  • And the trick for the is replacing the whole Rectangle. 而诀窍是替换整个矩形。 I took an ContentControl for this job 我为这项工作拿了一个ContentControl


<ContentControl>
                <ContentControl.Style>
                    <Style TargetType="{x:Type ContentControl}">
                    <Style.Triggers>
                            <DataTrigger Binding="{Binding Path=IsOnline}" Value="True">
                            <Setter Property="Content" >
                                <Setter.Value>
                                    <Rectangle Fill="Green" Width="20" Height="20">
                                        <Rectangle.Resources>
                                            <SolidColorBrush x:Key="BlackBrush" Color="Black" />
                                        </Rectangle.Resources>
                                        <Rectangle.OpacityMask>
                                            <VisualBrush Visual="{StaticResource appbar_disconnect}" Stretch="Fill" />
                                        </Rectangle.OpacityMask>
                                    </Rectangle>
                                </Setter.Value>
                            </Setter>
                        </DataTrigger>
                            <DataTrigger Binding="{Binding Path=IsOnline}" Value="False">
                            <Setter Property="Content" >
                                <Setter.Value>
                                    <Rectangle Fill="Red" Width="20" Height="20">
                                        <Rectangle.Resources>
                                            <SolidColorBrush x:Key="BlackBrush" Color="Black" />
                                        </Rectangle.Resources>
                                        <Rectangle.OpacityMask>
                                            <VisualBrush Visual="{StaticResource appbar_connect}" Stretch="Fill" />
                                        </Rectangle.OpacityMask>
                                    </Rectangle>
                                </Setter.Value>
                            </Setter>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </ContentControl.Style>
            </ContentControl>

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

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