c# - WPF 按钮在禁用时更改 Rectangle.OpacityMask

标签 c# wpf xaml

我无法管理我的问题的解决方案。

这是我的按钮

<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>

我的问题是如何设置我的触发器来更改 <VisualBrush Stretch="Fill" Visual="{StaticResource appbar_eye}" />当按钮未启用时。

最佳答案

创建一个以 Rectangle 类为目标的 Style,将 OpacityMask 移动到 Style 作为 Setter,否则 Style.Trigger 将无法更改本地值,并在 IsEnabled 属性为 false 时创建一个 Trigger 来更改 OpacityMask 到不同的 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>

关于c# - WPF 按钮在禁用时更改 Rectangle.OpacityMask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29773380/

相关文章:

c# - 连接到 SQL Anywhere - 未找到 dblgen12.dll

c# - 如何在 C#.NET 中使用 Moq 模拟在单次调用中多次调用的方法?

c# - `await HttpClient.GetAsync()` 静默失败 - DNX 命令行应用程序

xaml - UWP 网格中的内部项目未拉伸(stretch)

c# - 在 Xamarin Forms 编辑器中设置 CursorPosition

c# - 将资源分配给 CustomMessageBox.Content 时出现 ArgumentException

c# - 是否可以将变量绑定(bind)到集合项

c# - Wpf Prism 在导航后处理 ViewModel

WPF 替代品

xaml - 绑定(bind)到 Xamarin 表单中的附加属性