2010-03-31 72 views
4

我有一个ItemTemplate,其中是一个简单的按钮绑定在一个命令,可以执行或不取决于某些属性。如何根据绑定命令更改按钮背景颜色canexecute?

我想要这个按钮的背景颜色改变,如果命令不可执行。 我尝试了几种方法,但我无法找到纯粹在XAML中完成的操作(我在学习上下文中执行此操作,并且不允许使用代码)。

下面是我对按钮的代码:

<Button x:Name="Dispo" HorizontalAlignment="Center" Margin="0" VerticalAlignment="Center" Width="30" Height="30" 
     Grid.Column="2" Grid.Row="0" 
     Command="{Binding AddEmpruntCommandModel.Command}" 
     CommandParameter="{Binding ElementName='flowCars', Path='SelectedItem'}" 
     vm:CreateCommandBinding.Command="{Binding AddEmpruntCommandModel}" > 
    <Button.Style> 
     <Style TargetType="{x:Type Button}"> 
      <Style.Triggers> 
       <Trigger Property="IsEnabled" Value="True"> 
        <Setter Property="Button.Background" Value="Green"/> 
       </Trigger> 
       <Trigger Property="IsEnabled" Value="False"> 
        <Setter Property="Button.Background" Value="Red"/> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
    </Button.Style> 
    </Button> 

回答

0

您可以指定自己的模板那样:

<Button Content="OK" Command="{Binding SomeCommand}"> 
    <Button.Style> 
     <Style> 
      <Setter Property="Button.Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="Button"> 
         <Border x:Name="Border" Background="Green"> 
          <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" /> 
         </Border> 
         <ControlTemplate.Triggers>           
          <Trigger Property="IsEnabled" Value="false"> 
           <Setter TargetName="Border" Property="Background" Value="Red" /> 
          </Trigger> 
         </ControlTemplate.Triggers> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </Button.Style> 
</Button> 
相关问题