2016-12-09 25 views
0

こんにちは、私はWPFのデフォルトのmouseoverイベントをオーバーライドしようとしています。スタイルのボタンにコンテンツを追加する

私はそれを動作するように管理しますが、今、任意のテキストコンテンツ

スタイルのセッターを表示する問題を持っています

<Style x:Key="MyButton" TargetType="Button"> 
     <Setter Property="OverridesDefaultStyle" Value="True"/> 
     <Setter Property="Cursor" Value="Hand"/> 
     <Setter Property="Content" Value="Submit" /> 
     <Setter Property="Template"> 


      <Setter.Value> 
       <ControlTemplate TargetType="Button"> 
        <Border Name="Border" BorderThickness="0" BorderBrush="White" Background="#FF7AB800" Height="24" Margin="0,0,0.2,0" /> 



        <ControlTemplate.Triggers> 
         <Trigger Property="IsMouseOver" Value="True" > 

         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

HERESに私のボタンコード:すべての

<Button x:Name="submitBtn" HorizontalAlignment="Left" Margin="402,296,0,0" VerticalAlignment="Top" Width="75" Foreground="Black" FontFamily="Calibri Light" FontSize="16" Style="{StaticResource MyButton}" Content="Submit"/> 

答えて

0

ContentPresenterがありません。また、コンテンツの値を直接スタイルから削除することをお勧めします。コントロール内からこれをやりたいと思うでしょう。

<Style x:Key="MyButton" TargetType="Button"> 
    <Setter Property="OverridesDefaultStyle" Value="True"/> 
    <Setter Property="Cursor" Value="Hand"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="Button"> 
       <Border Name="Border" 
         BorderThickness="0" 
         BorderBrush="White" 
         Background="#FF7AB800" 
         Height="24" 
         Margin="0,0,0.2,0"> 
        <ContentPresenter Margin="2" 
          HorizontalAlignment="Center" 
          VerticalAlignment="Center" 
          RecognizesAccessKey="True" 
          Content="{TemplateBinding Content}"/> 
       </Border> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsMouseOver" Value="True" > 

        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

<Button x:Name="submitBtn" 
     HorizontalAlignment="Left" 
     Margin="402,296,0,0" 
     VerticalAlignment="Top" 
     Width="75" 
     Foreground="Black" 
     FontFamily="Calibri Light" 
     FontSize="16" 
     tyle="{StaticResource MyButton}" 
     Content="Submit"/> 
0

ファーストx:typeを追加する必要があります。

<Style x:Key="MyButton" TargetType="x:Type Button"> 
     <Setter Property="SnapsToDevicePixels" Value="True"/> 
     <Border Name="Border" BorderThickness="0" BorderBrush="White" Background="#FF7AB800" Height="24" Margin="0,0,0.2,0" /> 
      <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center" /> 
     </Border> 
       <ControlTemplate TargetType="x:Type Button"> 
関連する問題