Metro
Metro

Reputation: 1171

Can't use button property in trigger in WPF

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>

Upvotes: 0

Views: 1957

Answers (2)

Josh
Josh

Reputation: 10604

Here's a way to do it:

<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 only work in styles, so we needed to target the actual element that was receiving the style but bind to another element in the control.

Upvotes: 1

Dan Puzey
Dan Puzey

Reputation: 34200

IsEnabled is a DependecyProperty that's defined on a class that Button inherits from; XAML requires that you explicitly specify the owning type. Try Trigger Property="UIElement.IsEnabled" instead.

Upvotes: 3

Related Questions