2016-05-04 331 views
0

アイコンが付いたボタンがあり、無効にするとアイコンの色もグレー表示になります。私はスタイルトリガーを使用していくつかの異なる方法を疲れましたが、私はそれを働かせることはできません。私はWPFスタイリングには全く新しいので、どんな助けでも大歓迎です。ありがとうございました。WPFスタイルネストされた子要素のスタイルを更新するためのトリガー

ボタンコード:私は使用しています

<Button x:Name="btnNewProperty" Click="btnNewProperty_Click" Style="{StaticResource iconButton}" Width="145"> 
    <StackPanel> 
     <Viewbox> 
      <Path Data="M17,13H13V17H11V13H7V11H11V7H13V11H17M12,2A10,10 0 0,0 2,12A10,10 0 0,0 12,22A10,10 0 0,0 22,12A10,10 0 0,0 12,2Z" /> 
     </Viewbox> 
     <TextBlock>New Property</TextBlock> 
    </StackPanel> 
</Button> 

スタイル:

<Application.Resources> 

<SolidColorBrush x:Key="IconColor" Color="#FF444444" /> 
<SolidColorBrush x:Key="DisabledIconColor" Color="#FF999999" /> 

<Style x:Key="iconButton" TargetType="{x:Type Button}"> 
    <Setter Property="Margin" Value="5,5,5,5" /> 
    <Setter Property="Width" Value="120" /> 
    <Setter Property="Height" Value="25" /> 
    <Setter Property="FontSize" Value="14" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="Path"> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsEnabled" Value="False"> 
         <Setter Value="{StaticResource DisabledIconColor}" Property="Fill" /> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    <Style.Resources> 
     <Style TargetType="{x:Type StackPanel}"> 
      <Setter Property="Orientation" Value="Horizontal" /> 
     </Style> 
     <Style TargetType="{x:Type Viewbox}"> 
      <Setter Property="Width" Value="16" /> 
      <Setter Property="Height" Value="16" /> 
     </Style> 
     <Style TargetType="{x:Type Path}"> 
      <Setter Property="Margin" Value="-2,-4,0,0" /> 
      <Setter Property="Fill" Value="{StaticResource IconColor}" /> 
     </Style> 
     <Style TargetType="{x:Type TextBlock}"> 
      <Setter Property="Margin" Value="5,-2,0,0" /> 
      <Setter Property="Padding" Value="0,0,0,0" /> 
     </Style> 
    </Style.Resources>    
</Style> 

</Application.Resources> 

答えて

-1

くださいボタンのスタイル内でこの

<Button> 
    <Image Source="something.png"> 
    <Image.Style> 
     <Style TargetType="Image"> 
     <Style.Triggers> 
      <Trigger Property="IsEnabled" Value="False"> 
      <Setter Property="Opacity" Value="0.5" /> 
      </Trigger> 
     </Style.Triggers> 
     </Style> 
    </Image.Style> 
    </Image> 
</Button> 
0

てみてください私はVS 2013にコードを貼り付けると注目あなたのスタイル内で、テンプレートのセッターにはTargetTypeのパスがあります。これは許可されず、XAMLエディタにエラーが表示されます。ボタンのコントロールテンプレートが正しくセットアップされていません。ここでは、ボタンテンプレートの行動の利点を得ることを可能にするカスタムボタンの良い例へのリンクです:http://markheath.net/post/creating-custom-wpf-button-template-in

0

はあなたのボタンで

<Style TargetType="Button" x:Key="MyButton"> 
      <Setter Property="Background" Value="#FF444444" /> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type Button}"> 
         <StackPanel Background="{TemplateBinding Background}"> 
          <Viewbox> 
           <Path Data="M17,13H13V17H11V13H7V11H11V7H13V11H17M12,2A10,10 0 0,0 2,12A10,10 0 0,0 12,22A10,10 0 0,0 22,12A10,10 0 0,0 12,2Z" Fill="{TemplateBinding Background}"/> 
          </Viewbox> 
          <TextBlock>New Property</TextBlock> 
         </StackPanel> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 

      <Style.Triggers> 
       <Trigger Property="IsEnabled" Value="False"> 
        <Setter Property="Background" Value="#FF999999"/> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
をこのスタイルを試してみてください
関連する問題