2015-05-15 32 views
1

我試圖定義可以使用Style其中Button持有TextBlock作爲其ContentButtonIsEnabled =假我想設置TextBlockForegroung顏色。綁定TextBlock的顏色按鈕的IsEnabled自Style

<Button Style="{StaticResource TransparentButtonStyle}" 
     IsEnabled="{Binding IsAllowed}"> 
     <TextBlock Text="Click Me" 
       Style="{StaticResource HyperLinkStyle}"> 
     </TextBlock> 
</Button> 

<Style x:Key="TransparentButtonStyle" TargetType="{x:Type Button}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="Button"> 
       <Border Background="Transparent"> 
        <ContentPresenter/> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

<Style x:Key="HyperLinkStyle" TargetType="TextBlock"> 
    <Setter Property="Foreground" Value="Blue" /> 
    <Style.Triggers> 
     <Trigger Property="IsMouseOver" Value="True"> 
      <Setter Property="Cursor" Value="Hand" /> 
      <Setter Property="Foreground" Value="LightBlue" /> 
      <Setter Property="TextDecorations" Value="Underline" /> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

我可以修改TextBlockStyle搶父Button「不知何故小號IsEnabled值可以設置Foreground顏色?

回答

2

可以只需添加<Trigger Property="IsEnabled" Value="False">您HyperLinkStyle,但我建議你,而不是創造LinkBut​​tonStyle,所以你最終的標記填充是更清潔:

<Button Content="Link text" Style="{StaticResource LinkButtonStyle}" /> 

這只是讓你的意見乾淨多了...

,這裏是我的LinkBut​​ton模板:

<Style x:Key="LinkButtonStyle" TargetType="Button"> 
    <Setter Property="Foreground" Value="{StaticResource LinkButtonText}" /> 
    <Setter Property="Background" Value="Transparent" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="Button"> 
       <TextBlock x:Name="ContentPresenter" Background="{TemplateBinding Background}" 
          Text="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" 
          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> 

       <ControlTemplate.Triggers> 
        <Trigger Property="IsEnabled" Value="False"> 
         <Setter TargetName="ContentPresenter" Property="Foreground" Value="{DynamicResource LinkButtonDisabled}" /> 
        </Trigger> 
        <MultiTrigger> 
         <MultiTrigger.Conditions> 
          <Condition Property="IsMouseOver" Value="True" /> 
          <Condition Property="IsEnabled" Value="True" /> 
         </MultiTrigger.Conditions> 
         <Setter TargetName="ContentPresenter" Property="TextDecorations" Value="Underline"/> 
        </MultiTrigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 
2

如果你想獲得Button可以使用DataTriggerRalativeSource結合

<Style.Triggers> 
    <Trigger Property="IsMouseOver" Value="True"> 
     <!-- Setters --> 
    </Trigger> 
    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Button}}, Path=IsEnabled}" Value="False"> 
     <!-- Setters --> 
    </DataTrigger> 
</Style.Triggers> 

,但如果你想使你的Style獨立的什麼是父然後IsEnabled財產正常Trigger應該工作一樣好

<Style.Triggers> 
    <Trigger Property="IsMouseOver" Value="True"> 
     <!-- Setters --> 
    </Trigger> 
    <Trigger Property="IsEnabled" Value="False"> 
     <!-- Setters --> 
    </Trigger> 
</Style.Triggers> 

正常Trigger應該工作,因爲IsEnabled值由影響這

獲取成爲派生類 的IsEnabled的返回值的值... 此屬性的默認實現緩存值,並且還計算此元素的父元素是否啓用。 (如果未啓用父,子元素不能被有效地實用的用戶界面(UI)中啓用。)

所以基本上如果父母Button是殘疾兒童TextBlock將被停用