2017-11-09 18 views
0

テンプレートから値を変更します。WPF:私はXAMLで、次の「スタイル」を作成しました

<Style TargetType="Button" x:Key="iconButton"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="Button"> 
        <Grid Margin="5"> 
         <Ellipse Width="45" Height="45" Fill="White" Opacity="0"/> 
         <TextBlock Foreground="White" 
            HorizontalAlignment="Center" 
            VerticalAlignment="Center">hello</TextBlock> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
     <Style.Triggers> 
      <Trigger Property="IsMouseOver" Value="True"> 
       <!--TODO - change the "opacity" of the ellipse. 
       Maybe binding is necessary? --> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 

は私が(スタイルのテンプレート内にある)楕円の「不透明度」プロパティを変更したいですマウスでボタンを覆っています。

これはどのように達成できますか?

「バインディング」や「ダイナミックソース」を使用する必要がありますか?もしそうなら、どう?

+0

またのControlTemplateでTextBlock内のボタンのコンテンツを表示することもできます。 ''。それとも、TextBlockをContentPresenterで置き換えてください。 – Clemens

答えて

0

スタイルトリガーの代わりにControlTemplateトリガーを使用します。下記のコードを参照してください。

<Style TargetType="Button" x:Key="iconButton"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="Button"> 
         <Grid Margin="5"> 
          <Ellipse Width="45" x:Name="EllipseControl" Height="45" Fill="White" Opacity="0"/> 
          <TextBlock Foreground="White" 
           HorizontalAlignment="Center" 
           VerticalAlignment="Center">hello</TextBlock> 
         </Grid> 
         <ControlTemplate.Triggers> 
          <Trigger Property="IsMouseOver" Value="True"> 
           <Setter TargetName="EllipseControl" Property="Opacity" Value="1"/> 
          </Trigger> 
         </ControlTemplate.Triggers> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter>    
     </Style> 
関連する問題