绑定的IsEnabled属性无法工作

3

我有一个关于按钮的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}"/>

没有使用我的样式时,IsEnabled属性能够正常工作。但是如果我使用这个样式,按钮会一直处于启用状态。

我谷歌了好几个小时,但是什么都没找到 :-(

谢谢您的帮助!

2个回答

3

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

如果您想要它,请重新应用禁用外观。

MSDN有一个完整的按钮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 在他的答案中建议的方法


2

由于您正在替换按钮的模板,因此需要在自定义控件模板中处理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>

我已经尝试过了。但它不起作用。我的绑定属性甚至没有被调用一次,尽管我使用了OnPropertyChanged。 - Fruchtzwerg

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接