【问题标题】:Binding IsEnabled not Working绑定 IsEnabled 不起作用
【发布时间】:2015-08-17 14:34:17
【问题描述】:

我的按钮的 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,按钮总是启用的。 我用谷歌搜索了几个小时,但我什么也没找到:-(

感谢您的帮助!

【问题讨论】:

    标签: c# wpf button styles


    【解决方案1】:

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

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

    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

    【讨论】:

      【解决方案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>
      

      【讨论】:

      • 我已经试过了。但它不起作用。尽管我使用了 OnPropertyChanged,但我的绑定属性甚至没有被调用一次。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-21
      相关资源
      最近更新 更多