简体   繁体   中英

WPF Button change Rectangle.OpacityMask when disable

I am not able to manage a solution for my problem.

Here is my Button

<Button BorderBrush="Black" IsDefault="True" IsEnabled="{Binding ContentManager.CanPreview}" x:Name="Preview" Grid.Column="1" Style="{DynamicResource MetroCircleButtonStyle}">
    <Button.ToolTip>
        <ToolTip>
            <StackPanel>
                <TextBlock FontWeight="Bold">Preview</TextBlock>
                <TextBlock>Preview the selected document</TextBlock>
            </StackPanel>
        </ToolTip>
    </Button.ToolTip>
    <Rectangle Margin="1,0,0,0" Width="17" Height="12" Fill="Black">
        <Rectangle.OpacityMask>
            <VisualBrush Stretch="Fill" Visual="{StaticResource appbar_eye}" />
        </Rectangle.OpacityMask>
        <Rectangle.Style>
            <Style>
                <Style.Triggers>
                </Style.Triggers>
            </Style>
        </Rectangle.Style>
    </Rectangle>
</Button>

My question is how can I set my trigger to change the <VisualBrush Stretch="Fill" Visual="{StaticResource appbar_eye}" /> when the button is not enable.

Create a Style that targets Rectangle class, move OpacityMask to Style as Setter , otherwise Style.Trigger won't be able to change local value, and create a Trigger on IsEnabled property being false to change OpacityMask to a different Brush

<Rectangle Margin="1,0,0,0" Width="17" Height="12" Fill="Black">
   <Rectangle.Style>
      <Style TargetType="{x:Type Rectangle}">
         <Setter Property="OpacityMask">
            <Setter.Value>
               <VisualBrush Stretch="Fill" Visual="{StaticResource appbar_eye}" />
            </Setter.Value>
         </Setter>
         <Style.Triggers>
            <Trigger Property="IsEnabled" Value="False">
               <Setter Property="OpacityMask">
                  <Setter.Value>
                     <!-- other brush -->
                  </Setter.Value>
               </Setter>
            </Trigger>
         </Style.Triggers>
      </Style>
   </Rectangle.Style>
</Rectangle>

Maybe add the other visual brush as resource to your usercontrol/window/app

<Style.Resources>
            <VisualBrush x:Key="otherBrush" Stretch="Fill"  Visual="{StaticResource other_appbar_eye}" ></VisualBrush>

and use it in the trigger like this

<Rectangle.Style>
    <Style TargetType="Rectangle">
         <Style.Triggers>
              <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="OpacityMask" Value="{StaticResource otherBrush}"/>
              </Trigger>
          </Style.Triggers>
     </Style>
 </Rectangle.Style>

and move the default value to the style as well, since you´re not able to change it via trigger otherwise.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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