禁用CommandButton图标

时间:2019-02-27 20:40:05

标签: wpf xaml

我在WPF应用中有一个“刷新”按钮,附加到命令中

   <ToolBar>
        <Button Command="{Binding RefreshCommand}">
                <Viewbox Width="16" Height="16">
                    <Rectangle Width="16" Height="16">
                        <Rectangle.Fill>
                            <DrawingBrush>
                                <DrawingBrush.Drawing>
                                    <DrawingGroup>
                                        <DrawingGroup.Children>
                                            <GeometryDrawing Brush="#00FFFFFF" Geometry="F1M16,16L0,16 0,0 16,0z" />
                                            <GeometryDrawing Brush="#FFF6F6F6" Geometry="F1M16,8C16,12.411 12.411,16 8,16 3.589,16 0,12.411 0,8 0,6.597 0.384,5.212 1.088,4L0,4 0,0 8,0 8,8 4,8C4,10.206 5.794,12 8,12 10.206,12 12,10.206 12,8 12,6.656 11.331,5.41 10.21,4.666L9.377,4.112 11.592,0.78 12.425,1.333C14.663,2.822,16,5.314,16,8" />
                                            <GeometryDrawing Brush="#FF00529C" Geometry="F1M15,8C15,11.859 11.859,15 8,15 4.14,15 1,11.859 1,8 1,6.076 1.801,4.292 3.121,3L1,3 1,1 7,1 7,7 5,7 5,4.002C3.766,4.931 3,6.401 3,8 3,10.757 5.243,13 8,13 10.757,13 13,10.757 13,8 13,6.321 12.164,4.763 10.764,3.833L11.871,2.167C13.83,3.469,15,5.649,15,8" />
                                        </DrawingGroup.Children>
                                    </DrawingGroup>
                                </DrawingBrush.Drawing>
                            </DrawingBrush>
                        </Rectangle.Fill>
                    </Rectangle>
                </Viewbox>
        </Button>
    </ToolBar>

问题是,如果由于应用程序状态检查失败而导致RefreshCommand不可用,则图标不会发生任何变化(如果我有文本而不是图标,则可以无缝处理栏上的项目而不会任何其他所需的代码。

碰巧的是,我确实有一个XAML片段,我想将其用于变灰的isEnabled = false状态,我只需要知道如何告诉它使用上面的片段,即可将图标应用于isEnabled = true状态,并将另一个代码段设置为false。 (对不起,这个问题似乎很la脚。WPF对我来说还比较陌生。)

由于该图标来自VS图像库,因此我也将它们以bmp和png格式表示。不需要在xaml中绘制它。

为完整起见,以下是我想在不可用时替换的代码段。我现在注意到这只是一行不同,所以这可能会更容易(我希望)

<Viewbox Width="16" Height="16">
  <Rectangle Width="16" Height="16">
    <Rectangle.Fill>
      <DrawingBrush>
        <DrawingBrush.Drawing>
          <DrawingGroup>
            <DrawingGroup.Children>
              <GeometryDrawing Brush="#00FFFFFF" Geometry="F1M16,16L0,16 0,0 16,0z" />
              <GeometryDrawing Brush="#FFF6F6F6" Geometry="F1M16,8C16,12.411 12.411,16 8,16 3.589,16 0,12.411 0,8 0,6.597 0.384,5.212 1.088,4L0,4 0,0 8,0 8,8 4,8C4,10.206 5.794,12 8,12 10.206,12 12,10.206 12,8 12,6.656 11.331,5.41 10.21,4.666L9.377,4.112 11.592,0.78 12.425,1.333C14.663,2.822,16,5.314,16,8" />
              <GeometryDrawing Brush="#FF424242" Geometry="F1M15,8C15,11.859 11.859,15 8,15 4.14,15 1,11.859 1,8 1,6.076 1.801,4.292 3.121,3L1,3 1,1 7,1 7,7 5,7 5,4.002C3.766,4.931 3,6.401 3,8 3,10.757 5.243,13 8,13 10.757,13 13,10.757 13,8 13,6.321 12.164,4.763 10.764,3.833L11.871,2.167C13.83,3.469,15,5.649,15,8" />
            </DrawingGroup.Children>
          </DrawingGroup>
        </DrawingBrush.Drawing>
      </DrawingBrush>
    </Rectangle.Fill>
  </Rectangle>
</Viewbox>

2 个答案:

答案 0 :(得分:2)

如Ed Plunkett所建议,您可以在触发器中设置内容。

这是如何执行此操作的完整示例。我已将按钮的IsEnabled属性绑定到复选框以对其进行测试。

请记住,如果要在触发器中修改属性,则必须使用样式的Setter设置该属性,而不是将其设置为常规属性。

<Window x:Class="ButtonTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ButtonTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="80" Width="800">
  <Grid>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="*"/>
      <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

    <Button Grid.Column="0" IsEnabled="{Binding IsChecked, ElementName=cb}" >
      <Button.Style>
        <Style TargetType="Button">

          <Setter Property="Content">
            <Setter.Value>
              <Viewbox Width="16" Height="16">
                <Rectangle Width="16" Height="16">
                  <Rectangle.Fill>
                    <DrawingBrush>
                      <DrawingBrush.Drawing>
                        <DrawingGroup>
                          <DrawingGroup.Children>
                            <GeometryDrawing Brush="#00FFFFFF" Geometry="F1M16,16L0,16 0,0 16,0z" />
                            <GeometryDrawing Brush="#FFF6F6F6" Geometry="F1M16,8C16,12.411 12.411,16 8,16 3.589,16 0,12.411 0,8 0,6.597 0.384,5.212 1.088,4L0,4 0,0 8,0 8,8 4,8C4,10.206 5.794,12 8,12 10.206,12 12,10.206 12,8 12,6.656 11.331,5.41 10.21,4.666L9.377,4.112 11.592,0.78 12.425,1.333C14.663,2.822,16,5.314,16,8" />
                            <GeometryDrawing Brush="#FF00529C" Geometry="F1M15,8C15,11.859 11.859,15 8,15 4.14,15 1,11.859 1,8 1,6.076 1.801,4.292 3.121,3L1,3 1,1 7,1 7,7 5,7 5,4.002C3.766,4.931 3,6.401 3,8 3,10.757 5.243,13 8,13 10.757,13 13,10.757 13,8 13,6.321 12.164,4.763 10.764,3.833L11.871,2.167C13.83,3.469,15,5.649,15,8" />
                          </DrawingGroup.Children>
                        </DrawingGroup>
                      </DrawingBrush.Drawing>
                    </DrawingBrush>
                  </Rectangle.Fill>
                </Rectangle>
              </Viewbox>
            </Setter.Value>
          </Setter>

          <Style.Triggers>
            <Trigger Property="IsEnabled" Value="false">
              <Setter Property="Content">
                <Setter.Value>
                  <Viewbox Width="16" Height="16">
                    <Rectangle Width="16" Height="16">
                      <Rectangle.Fill>
                        <DrawingBrush>
                          <DrawingBrush.Drawing>
                            <DrawingGroup>
                              <DrawingGroup.Children>
                                <GeometryDrawing Brush="#00FFFFFF" Geometry="F1M16,16L0,16 0,0 16,0z" />
                                <GeometryDrawing Brush="#FFF6F6F6" Geometry="F1M16,8C16,12.411 12.411,16 8,16 3.589,16 0,12.411 0,8 0,6.597 0.384,5.212 1.088,4L0,4 0,0 8,0 8,8 4,8C4,10.206 5.794,12 8,12 10.206,12 12,10.206 12,8 12,6.656 11.331,5.41 10.21,4.666L9.377,4.112 11.592,0.78 12.425,1.333C14.663,2.822,16,5.314,16,8" />
                                <GeometryDrawing Brush="#FF424242" Geometry="F1M15,8C15,11.859 11.859,15 8,15 4.14,15 1,11.859 1,8 1,6.076 1.801,4.292 3.121,3L1,3 1,1 7,1 7,7 5,7 5,4.002C3.766,4.931 3,6.401 3,8 3,10.757 5.243,13 8,13 10.757,13 13,10.757 13,8 13,6.321 12.164,4.763 10.764,3.833L11.871,2.167C13.83,3.469,15,5.649,15,8" />
                              </DrawingGroup.Children>
                            </DrawingGroup>
                          </DrawingBrush.Drawing>
                        </DrawingBrush>
                      </Rectangle.Fill>
                    </Rectangle>
                  </Viewbox>
                </Setter.Value>
              </Setter>
            </Trigger>
          </Style.Triggers>

        </Style>
      </Button.Style>

    </Button>

    <CheckBox x:Name="cb" Content="Enabled" Grid.Column="1"/>

  </Grid>
</Window>

答案 1 :(得分:1)

您可以使用Triggers基于Content属性值来设置IsEnabled

<Button Command="{Binding RefreshCommand}">
    <Button.Style>
        <Style TargetType="Button">
            <Style.Triggers>
                <Trigger Property="IsEnabled" Value="True">
                    <Setter Property="Content">
                        <Setter.Value>
                            <!--Your drawing / image when it's enabled-->
                        </Setter.Value>
                    </Setter>
                </Trigger>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="Content">
                        <Setter.Value>
                            <!--Your drawing / image when it's disabled-->
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>