2013-03-30 86 views
0

我只是想让我的Button模板中的Label看起来被禁用。浏览互联网,这似乎是一个简单的任务,我做错了什么? (由RV1987使用应答后完整的代码)WPF基于父命令启用的样式子控件

<Button Command="{Binding CommandProcess}"> 
    <StackPanel Orientation="Horizontal"> 
     <Image Source="Images\Cloud-Upload.png"/> 

     <Label Content="Upload and Process" Foreground="White" VerticalAlignment="Center" FontWeight="Bold" FontSize="18.667" Margin="5,0,0,0"> 
      <Label.Style> 
       <Style TargetType="Label"> 
        <Style.Triggers> 
         <DataTrigger Binding="{Binding Path=IsEnabled, RelativeSource={RelativeSource AncestorType={x:Type Button}}}" Value="False"> 
          <Setter Property="Foreground" Value="Gray"></Setter> 
          <Setter Property="ToolTip" Value="Please select a record type for each file selected for processing"></Setter> 
         </DataTrigger> 
        </Style.Triggers> 
       </Style> 
      </Label.Style> 
     </Label> 
    </StackPanel> 
</Button> 

EDIT:

<Button Command="{Binding CommandProcess}" x:Name="ProcessButton"> 
    <StackPanel Orientation="Horizontal"> 
     <Image Source="Images\Cloud-Upload.png"/> 

     <Label Content="Upload and Process" VerticalAlignment="Center" FontWeight="Bold" FontSize="18.667" Margin="5,0,0,0"> 
      <Label.Style> 
       <Style TargetType="Label"> 
        <Style.Triggers> 
         <DataTrigger Binding="{Binding Path=IsEnabled, ElementName=ProcessButton}" Value="True"> 
          <Setter Property="Foreground" Value="White"></Setter> 
         </DataTrigger> 

         <DataTrigger Binding="{Binding Path=IsEnabled, ElementName=ProcessButton}" Value="False"> 
          <Setter Property="Foreground" Value="Gray"></Setter> 
          <Setter Property="ToolTip" Value="Please select a record type for each file selected for processing"></Setter> 
         </DataTrigger> 
        </Style.Triggers> 
       </Style> 
      </Label.Style> 
     </Label> 
    </StackPanel> 
</Button> 

回答

1

使用ElementName代替。 RelativeSource = FindAncestor在这里不起作用,因为按钮不在Visual Tree中,而是它的StackPanel的兄弟。给name to button和使用ElementName您的结合使用它 -

<Button Command="{Binding CommandProcess}" x:Name="MyButton"> 

和DataTrigger -

<DataTrigger Binding="{Binding Path=IsEnabled, ElementName=MyButton}" 
      Value="False"> 
    ..... 
</DataTrigger> 

Visual Tree结构,标签和按钮是这样的 -

Label <--- StackPanel <--- StackPanel's Parent 
Button <--- StackPanel's Parent 

由于可能看到Button是StackPanel的兄弟,它现在存在于标签的Visual树中,这就是为什么FindAncestor不会让你到Button

+0

完美的感谢,不得不为True添加一个“白色”二传手,但效果很好,再次感谢 –