2013-02-21 108 views
-1

我创建了一个ResourceDictionary以便为Button创建一个ControlTemplate参数和资源字典

这里是我的XAML代码

<ControlTemplate TargetType="{x:Type Button}" x:Key="BoutonRessources"> 
<Button Width="32" Margin="0,0,7,0" Name="tbrClear" ToolTip="Clear" VerticalAlignment="Center" BorderThickness="0" HorizontalAlignment="Center" Background="Transparent" HorizontalContentAlignment="Center"> 
    <Button.Content> 
     <Border> 
      <Image Source="xRtDiva_XWPF_TBR_PREMIER.PNG_IMAGES.png" Height="18"/> 
      <Border.Style> 
       <Style TargetType="{x:Type Border}"> 
        <Setter Property="Background" Value="Transparent" /> 
        <Style.Triggers> 
         <Trigger Property="IsMouseOver" Value="True"> 
          <Setter Property="Background"> 
           <Setter.Value> 
            <ImageBrush ImageSource="BoutonToolbarSelected.png"/> 
           </Setter.Value> 
          </Setter> 
          <Setter Property="Height" Value="22"/> 
          <Setter Property="Width" Value="32"/> 
         </Trigger> 
        </Style.Triggers> 
       </Style> 
      </Border.Style> 
     </Border> 
    </Button.Content> 
    <Button.Style> 
     <Style TargetType="{x:Type Button}" > 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type Button}" > 
         <ContentPresenter /> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </Button.Style> 
</Button> 
</ControlTemplate> 

当我在我的XAML代码Y用的模板:

<Button Template="{StaticResource BoutonRessources}"> 

我想给一个参数,以设置图像为这个模板“BoutonRessources”。

我试图改变这样的我的代码:

<Button Template="{StaticResource BoutonRessources}"> 
    <Button.Content> 
     <Border> 
      <Image Source="myNewPicture.png" Height="18"/> 
     </Border> 
    </Button.Content> 
</Button> 

我怎样才能个性化我的按钮模板的图片吗?

感谢很多:)

+0

您想在什么条件下显示不同的图像?图像是您希望按钮显示的唯一内容,还是您希望显示其他内容(如文本)? – 2013-02-21 21:13:23

+0

只是一个图像:)我想能够通过“myPicture.png”改变=“xRtDiva_XWPF_TBR_PREMIER.PNG_IMAGES.png”就像我的例子:) – 2013-02-21 21:53:02

回答

0

在你的按钮模板时,您将您的按钮为固定值的内容,这样的分配按钮本身不会做任何事情的内容。在您的模板中的某处,您需要添加ContentPresenter。如果您这样做,则ContentPresenter将被您分配按钮内容的任何内容所取代。

你想完成什么?你想要的按钮有2个图像或1?也许看看这个关于创建模板的教程(或者其他众多模板之一),看看它是否有帮助:http://mark-dot-net.blogspot.com/2007/07/creating-custom-wpf-button-template-in.html

作为一个简单的例子,对模板的这种小修改会添加添加到内容中的图像next到您在模板中手动分配的图像。注意我已经添加了一个StackPanel并在模板中放置了一个ContentPresenter。这将分配给该按钮的内容替换,并且会显示在右侧的“xRtDiva ......”你的形象在模板设置:

<ControlTemplate TargetType="{x:Type Button}" x:Key="BoutonRessources"> 
     <Button Width="50" Margin="126.5,126,115.5,126" Name="tbrClear" ToolTip="Clear" VerticalAlignment="Center" BorderThickness="0" HorizontalAlignment="Center" Background="Transparent" HorizontalContentAlignment="Center"> 
      <StackPanel Orientation="Horizontal"> 
       <Border> 
        <Image Source="xRtDiva_XWPF_TBR_PREMIER.PNG_IMAGES.png" Height="18"/> 
        <Border.Style> 
         <Style TargetType="{x:Type Border}"> 
          <Setter Property="Background" Value="Transparent" /> 
          <Style.Triggers> 
           <Trigger Property="IsMouseOver" Value="True"> 
            <Setter Property="Background"> 
             <Setter.Value> 
              <ImageBrush ImageSource="BoutonToolbarSelected.png"/> 
             </Setter.Value> 
            </Setter> 
            <Setter Property="Height" Value="22"/> 
            <Setter Property="Width" Value="32"/> 
           </Trigger> 
          </Style.Triggers> 
         </Style> 
        </Border.Style> 
       </Border> 
       <Border> 
        <ContentPresenter/> 
       </Border> 
      </StackPanel> 

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

     </Button> 
    </ControlTemplate> 

我对Button.Style很迷茫但是,您已将按钮分配给按钮。那是什么目的?

+0

你好,我想要什么?我只是想能够更改“xRtDiva_XWPF_TBR_PREMIER.PNG_IMAGES.png”:) – 2013-02-21 21:26:10

+0

那么你想它默认为xRtDiva ...然后,如果你设置按钮本身的内容它覆盖默认?如果你只是想创建一个具有(可定制)图像内容的按钮,我建议看看这个答案:http://stackoverflow.com/questions/2635344/image-button-template – 2013-02-21 21:29:00

+0

不,它不会不会覆盖默认值。你可以请给我的例子,我不明白你给我提供的答案链接:(非常感谢:) – 2013-02-21 21:32:12

0

为什么你不能只做以下?

<Button> 
    <Image Source="firstImage.png" /> 
</Button> 

<Button> 
    <Image Source="secondImage.png" /> 
</Button> 

<Button> 
    <Image Source="thirdImage.png" /> 
</Button> 

如果您想更改按钮模板这种情况下,只要把你想要的形象是一个ContentPresenter元素。

+0

请问我可以在我的模板上插入ContentPresenter的位置吗?非常感谢 :) – 2013-02-21 22:03:48