Fruchtzwerg
Fruchtzwerg

Reputation: 11389

Binding IsEnabled not Working

I´m having a Problem with the IsEnabled property of my Buttons.

This is my Button Style:

        <Style x:Key="Button_selectable" TargetType="{x:Type Button}">
        <Setter Property="FontSize" Value="15"/>
        <Setter Property="SnapsToDevicePixels" Value="True"/>

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border CornerRadius="5" Background="{TemplateBinding Background}" BorderThickness="1" BorderBrush="Black" Margin="1,1,1,1">
                        <Grid>
                            <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=Tag, RelativeSource={RelativeSource Self}}" Value="True">
                <Setter Property="Background" Value="MediumSlateBlue"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding Path=Tag, RelativeSource={RelativeSource Self}}" Value="False">
                <Setter Property="Background" Value="white"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>

Usage:

<Button Command="{Binding command}" IsEnabled="{Binding Enable}" x:Name="button" Content="Button1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" FontSize="13.333" Style="{StaticResource Button_selectable}" Tag="{Binding State}"/>

Without my Sytle the IsEnabled property is working correct. But if I use this Sytle, the Button is always Enabled. I googled hours,... but I found nothing :-(

Thanks for help!

Upvotes: 3

Views: 1685

Answers (2)

Rachel
Rachel

Reputation: 132548

You're overwriting the control's Template, so the default template (which applies the disabled appearance) is no longer there.

You'll need to re-apply the disabled appearance it you want it.

MSDN has an example of a full Button ControlTemplate with the different states if you want some sample code. I've also copied the relevant bit of XAML for the Disabled state below as an example.

<ControlTemplate TargetType="Button">
    <Border TextBlock.Foreground="{TemplateBinding Foreground}"
            x:Name="Border"
            CornerRadius="2"
            BorderThickness="1">

      <VisualStateManager.VisualStateGroups>
          <VisualState x:Name="Disabled">
            <Storyboard>
              <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).
                  (GradientBrush.GradientStops)[1].(GradientStop.Color)"
                                            Storyboard.TargetName="Border">
                <EasingColorKeyFrame KeyTime="0"
                                     Value="{StaticResource DisabledControlDarkColor}" />
              </ColorAnimationUsingKeyFrames>
              <ColorAnimationUsingKeyFrames
                  Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)"
                                            Storyboard.TargetName="Border">
                <EasingColorKeyFrame KeyTime="0"
                                     Value="{StaticResource DisabledForegroundColor}" />
              </ColorAnimationUsingKeyFrames>
              <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).
                  (GradientBrush.GradientStops)[1].(GradientStop.Color)"
                                            Storyboard.TargetName="Border">
                <EasingColorKeyFrame KeyTime="0"
                                     Value="{StaticResource DisabledBorderDarkColor}" />
              </ColorAnimationUsingKeyFrames>
            </Storyboard>
          </VisualState>
        </VisualStateGroup>
      </VisualStateManager.VisualStateGroups>
      <ContentPresenter Margin="2"
                        HorizontalAlignment="Center"
                        VerticalAlignment="Center"
                        RecognizesAccessKey="True" />
    </Border>
</ControlTemplate>

Alternatively, you could just handle it yourself in your ControlTemplate with some other method, like Glen Thomas suggests in his answer

Upvotes: 3

Glen Thomas
Glen Thomas

Reputation: 10744

Because you are replacing the Button's template, you will need to handle the IsEnabled within your custom control template.

<Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border CornerRadius="5" Background="{TemplateBinding Background}" BorderThickness="1" BorderBrush="Black" Margin="1,1,1,1"
IsEnabled="{TemplateBinding IsEnabled}">
                        <Grid>
                            <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>

Upvotes: 2

Related Questions