2015-08-17 4 views
3

У меня проблема с свойством IsEnabled моих кнопок.Binding 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, кнопка всегда включена. Я гугл часов, ... но я ничего не нашел :-(

Спасибо за помощь!

ответ

3

вы перезапись шаблон элемента управления, поэтому шаблон по умолчанию (который применяется отключенный внешний вид) больше не существует.

Вам необходимо повторно применить инвалидов внешний вид это вы хотите.

MSDN has an example of a full Button ControlTemplate с различными состояниями, если вы хотите получить код образца. В качестве примера я также скопировал соответствующий бит XAML для состояния Disabled ниже.

<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

Поскольку вы заменяете шаблон этой кнопки, вам нужно будет обрабатывать 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> 
+0

Я попытался это уже. Но это не работает. Мое свойство привязки даже не называется однажды, хотя я использую OnPropertyChanged. – Fruchtzwerg

+0

Ну, это, наверное, еще один вопрос ... –

Смежные вопросы