绑定IsEnabled无法正常工作

时间:2015-08-17 14:34:17

标签: c# wpf button styles

我的Buttons的IsEnabled属性出现问题。

这是我的按钮样式:

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

用法:

<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}"/>

没有我的Sytle,IsEnabled属性工作正常。但是如果我使用这个Sytle,Button总是启用。 我用谷歌搜索了几个小时......但我什么也没找到: - (

感谢您的帮助!

2 个答案:

答案 0 :(得分:3)

您正在覆盖控件的模板,因此默认模板(应用禁用的外观)不再存在。

您需要重新应用所需的禁用外观。

如果您需要一些示例代码,请使用不同的状态

MSDN has an example of a full Button ControlTemplate。我还将下面的禁用状态XAML的相关位复制为一个例子。

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

或者,您可以使用其他方法在自己的ControlTemplate中自行处理,例如Glen Thomas suggests in his answer

答案 1 :(得分:2)

因为您要替换Button的模板,所以您需要在自定义控件模板中处理IsEnabled。

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