2012-04-04 18 views
0
<Style x:Key="MyButton" TargetType="{x:Type Border}"> 
     <Style.Resources> 
      <Style TargetType="{x:Type TextBlock}">      
       <Setter Property="FontSize" Value="24"/> 
       <Setter Property="Foreground" Value="{StaticResource FontColor}"/>      
       <Setter Property="HorizontalAlignment" Value="Center"/> 
       <Setter Property="Cursor" Value="Hand"/> 
       <Setter Property="FontWeight" Value="DemiBold" /> 
       <Setter Property="Effect"> 
        <Setter.Value> 
         <BlurEffect Radius="2" ></BlurEffect> 
        </Setter.Value> 
       </Setter> 
       <Style.Triggers> 
        <Trigger Property="IsMouseOver" Value="True"> 
         <Setter Property="FontWeight" Value="ExtraBold" /> 
        </Trigger>             
       </Style.Triggers> 
      </Style> 
     </Style.Resources> 
    </Style> 

ボーダーとテキストブロックで構成される独自のボタンスタイルを作成したいと思います。自分のスタイルにコントロールを追加するにはどうしたらいいですか?私は実際に余分なテキストブロックを追加して、背面にあるテキストボックスをぼかすことができるようにします。スタイルwpfにコントロールを追加する

答えて

2

2番目のぼかしテキストブロックの見た目を正確にはわかりませんが、次のxamlは後でボタンテンプレートを作成する上でうまくいくはずです。

各テキストブロックと同様に、ボーダー、あなたがそれらを個別にスタイルとあなたのボタンのスタイルのコントロールテンプレート内でそれらすべてを包むことができるように、独自のスタイルを持っている:

<Window x:Class="WpfApplication1.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525"> 
<Window.Resources> 
    <Style x:Key="MainTextBlock" TargetType="{x:Type TextBlock}"> 
     <Setter Property="FontSize" Value="24"/> 
     <Setter Property="Foreground" Value="Blue"/> 
     <Setter Property="HorizontalAlignment" Value="Center"/> 
     <Setter Property="Cursor" Value="Hand"/> 
     <Setter Property="FontWeight" Value="DemiBold" /> 
     <Style.Triggers> 
      <Trigger Property="IsMouseOver" Value="True"> 
       <Setter Property="FontWeight" Value="ExtraBold" /> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 

    <Style x:Key="BlurTextBlock" TargetType="{x:Type TextBlock}"> 
     <Setter Property="FontSize" Value="24"/> 
     <Setter Property="Foreground" Value="Blue"/> 
     <Setter Property="HorizontalAlignment" Value="Center"/> 
     <Setter Property="Cursor" Value="Hand"/> 
     <Setter Property="FontWeight" Value="DemiBold" /> 
     <Setter Property="Effect"> 
      <Setter.Value> 
       <BlurEffect Radius="2" ></BlurEffect> 
      </Setter.Value> 
     </Setter> 
    </Style> 

    <Style x:Key="BorderStyle" TargetType="Border"> 
     <Setter Property="BorderBrush" Value="Orange" /> 
     <Setter Property="BorderThickness" Value="2" /> 
    </Style> 

    <Style x:Key="MyButton" TargetType="Button"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="Button"> 
        <Border Style="{StaticResource BorderStyle}"> 
         <Grid> 
          <TextBlock Style="{StaticResource MainTextBlock}" Text="{TemplateBinding Content}" /> 
          <TextBlock Style="{StaticResource BlurTextBlock}" /> 
         </Grid> 
        </Border> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</Window.Resources> 
<Grid> 
    <Button Style="{StaticResource ResourceKey=MyButton}" Height="30" Width="100" Content="BUTTON" /> 
</Grid></Window> 
0

ボタンはありません。 Textプロパティには、Contentプロパティがあります。

Contentプロパティは文字列でもかまいませんが、常に文字列であるとは限りません。

問題は、コンテンツのスタイルを設定したいが、できないことです。それはコンテナです。イメージ、グリッド、または全体のxaml階層をContentプロパティに追加できます。

コンテンツにTextBlockを追加する必要があります。

<Button HorizontalAlignment="Left" Margin="190,113,0,0" VerticalAlignment="Top" Width="75"> 
    <Button.Content> 
     <Grid> 
      <TextBlock Text="Click Me!" /> 
      <TextBlock Text="Click Me!"> 
       <TextBlock.Effect> 
        <BlurEffect Radius="2" /> 
       </TextBlock.Effect> 
      </TextBlock> 
     </Grid> 
    </Button.Content> 
</Button> 

は、これはそれだけでButton.Contentプロパティにpropertコンテンツを追加するために、あなたを必要とし、スタイルを必要としていないようです。

関連する問題