0

I have button with custom content (a red circle) defined inside a user control and I want the circle to change to gray if the button is disabled.

I tried the following and it does not compile

Error: 'IsEnabled' member is not valid because it does not have a qualifying type name.

Can any explain what I am doing wrong here?

<Button>
    <Button.Content>
        <Grid>
             <Ellipse x:Name="circle" Width="20" Height="20" Fill="Red"/>
        </Grid>
     </Button.Content>
     <Button.Triggers>
         <Trigger Property="IsEnabled" Value="False">
        <Setter TargetName="circle" Property="Fill" Value="Gray"/>
          </Trigger>
      </Button.Triggers>
</Button>
4

2 回答 2

3

IsEnabled是在继承自的类上定义的 DependecyProperty Button;XAML 要求您明确指定拥有类型。试试Trigger Property="UIElement.IsEnabled"吧。

于 2012-07-31T14:06:08.850 回答
1

这是一种方法:

<Button Name="btn" >
    <Ellipse Height="20" Width="20">
        <Ellipse.Style>
            <Style TargetType="Ellipse">
                <Setter Property="Fill" Value="Red" />
                <!-- Here is the 'normal' content -->
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsEnabled, ElementName=btn}" Value="False">
                    <Setter Property="Fill" Value="Blue" />
                    <!-- Here is the 'override' content -->
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Ellipse.Style>
    </Ellipse>
</Button>

DataTriggers 仅适用于样式,因此我们需要定位接收样式但绑定到控件中的另一个元素的实际元素。

于 2012-07-31T16:14:19.123 回答