首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >绑定IsEnabled无效

绑定IsEnabled无效
EN

Stack Overflow用户
提问于 2015-08-17 14:34:17
回答 2查看 1.3K关注 0票数 3

我的按钮的IsEnabled属性出了问题。

这是我的按钮风格:

代码语言:javascript
复制
        <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>

用法:

代码语言:javascript
复制
<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总是被启用的。我谷歌了几个小时..。但我什么也没找到-

谢谢你帮忙!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-08-17 14:42:47

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

您需要重新应用禁用的外观,这是您想要的。

如果您需要一些示例代码,则使用不同状态的MSDN有一个完整按钮ControlTemplate的示例。。作为示例,我还复制了以下禁用状态的相关XAML位。

代码语言:javascript
复制
<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中使用其他方法来处理它,比如格伦·托马斯在他的回答中建议

票数 3
EN

Stack Overflow用户

发布于 2015-08-17 14:40:08

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

代码语言:javascript
复制
<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>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32053113

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档