2013-03-30 28 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添加一個「白色」二傳手,但效果很好,再次感謝 –