2016-08-25 32 views
1

我有一个自定义按钮Style写在XAML。它是一个带有图像和文字的按钮。 但Image应该是可定制的。我需要更改设计器中的Source属性。如何在XAML中更改资源模板的子值?

我的代码:

<Window.Resources> 
    <ResourceDictionary> 
    <Style x:Key="SSbutton" TargetType="{x:Type Button}"> 
      <Setter Property="Cursor" Value="Hand"/> 
      <Setter Property="Background" Value="Green"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="Button"> 
        <Viewbox Stretch="Uniform"> 
          <Border Background="{TemplateBinding Background}" Width="{TemplateBinding Width}" 
           Height="{TemplateBinding Height}"> 
           <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Center"> 
            <!--I want to change this Source property--> 
            <Image Source="img/desktop.png" Width="30" HorizontalAlignment="Left" /> 
            <TextBlock Margin="3,0,0,0" HorizontalAlignment="Right" VerticalAlignment="Center" 
              Text="{TemplateBinding Content}" FontSize="{TemplateBinding FontSize}"/> 
           </StackPanel> 
          </Border> 
         </Viewbox> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
    </ResourceDictionary> 
</Window.Resources> 
<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="25"/> 
     <RowDefinition /> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="90" /> 
     <ColumnDefinition /> 
    </Grid.ColumnDefinitions> 
    <Border Grid.Column="0" Grid.Row="1" Background="LightGreen"> 
    <StackPanel > 
     <Button Style="{StaticResource SSbutton}" Width="90" Height="30" Content="Desktop" FontSize="13" 
       Foreground="White"/> 
    </StackPanel> 
    </Border> 
</Grid> 

我怎么能这样做?

回答

1

使用任意的模板绑定与方便丹迪Tag属性piggy回到属性;

<Style x:Key="SSbutton" TargetType="{x:Type Button}"> 
      <Setter Property="Cursor" Value="Hand"/> 
      <Setter Property="Background" Value="Green"/> 
      <!-- Set a default --> 
      <Setter Property="Tag" Value="img/desktop.png"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="Button"> 
        <Viewbox Stretch="Uniform"> 
          <Border Background="{TemplateBinding Background}" Width="{TemplateBinding Width}" 
           Height="{TemplateBinding Height}"> 
           <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Center"> 
            <!--I want to change this Source property--> 
            <Image Source="{Binding Path=Tag, RelativeSource={RelativeSource TemplatedParent}}" Width="30" HorizontalAlignment="Left" /> 
            <TextBlock Margin="3,0,0,0" HorizontalAlignment="Right" VerticalAlignment="Center" 
              Text="{TemplateBinding Content}" FontSize="{TemplateBinding FontSize}"/> 
           </StackPanel> 
          </Border> 
         </Viewbox> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

然后在实例;

<Button Style="{StaticResource SSbutton}" 
     Tag="Some/Other/Image.png" 
     Width="90" Height="30" 
     Content="Desktop" 
     FontSize="13" Foreground="White"/> 

希望这会有所帮助,欢呼声。

编辑:根据OP的注释更新以反映wpf中模板绑定的路径注意事项。

+0

你试过了吗?我无法让它工作。我做了复制粘贴,但没有显示图像。难道我做错了什么? –

+0

我在http://stackoverflow.com/questions/4638741/template-binding-in-control-template找到了一个相关的问题,并将'{TemplateBinding Tag}'代码改为了{{Binding Path = Tag,RelativeSource = {RelativeSource TemplatedParent}}'。所以它运作良好。如果您使用此方法或您的工作更新您的问题,我会接受它。 **注意**:但是这种方法只在运行时才起作用。图像仅在运行时显示,而非设计时间。 –

+1

啊对不起,我没有注意到x:键入当我复制/粘贴你的模板,并认为它是赢得普遍的,因为没有问题上的WPF标签。如果将图像的构建操作设置为资源并使用pack uri而不是文字文件路径,则它应该在设计时也能工作。 –