2014-07-07 204 views
0

下面是我对按钮的代码:水平对齐

<Button //namespaces 
     x:Class="myProject.MyButton" 
     FocusVisualStyle="{x:Null}" 
     Foreground="White" 
     Height="60" 
     Width="200" 
     HorizontalContentAlignment="Center" 
     VerticalContentAlignment="Center" 
     Padding="0 0 1 1" 
     > 
    <Button.Template> 
     <ControlTemplate TargetType="{x:Type Button}"> 
      <Border x:Name="border" CornerRadius="8"> 
       <Border.Background> 
        #006BB7 
       </Border.Background> 
       <DockPanel Margin="3"> 
        <Image Source="{Binding Path=Tag, RelativeSource={RelativeSource TemplatedParent}}" Height="30" Stretch="Fill" Margin="0 0 0 0" HorizontalAlignment="Left"/> 
        <TextBlock Text="{Binding Path=Content,RelativeSource={RelativeSource TemplatedParent}}" FontSize="20" VerticalAlignment="Center" Padding="10,0,10,0" HorizontalAlignment="Left"/> 
        <Image Source="{DynamicResource ResourceKey=Bmp}" Height="30" Width="30" HorizontalAlignment="Right" Margin="0 0 10 0"></Image> 
       </DockPanel> 
      </Border> 
     </ControlTemplate> 
    </Button.Template> 
</Button> 

当我这样称呼它:

<ui:MyButton x:Name="btnGo" Tag="/Resources/Go.png" Content="Go" Command="{Binding Path=GoCommand}" Margin="20 0 0 0"/> 

,一切看起来像我希望它看起来:第一图像ISN不显示,按钮的第三部分(第二个图像)右对齐。

然而,在这里:

<ui:MyButton Margin="10" Content="{Binding MyName}" Tag="{Binding Path}" HorizontalAlignment="Center" VerticalAlignment="Top"> 
    <Button.Resources> 
     <BitmapImage x:Key="Bmp" UriSource="/Images/arrowRight.png"></BitmapImage> 
    </Button.Resources> 
</ui:MyButton> 

WHN我这样调用按钮,文本和第二图像不显示。所有显示的是第一个图像,并通过整个按钮的宽度拉伸。这是为什么发生?我想让这个行为像另一个行为一样。

+1

,然后添加适当的'DependencyProperty'来绑定到你的两个'ImageSource'属性。 – Sheridan

+1

@Sheridan我没有定义按钮的类。 –

+0

那么这是什么:'

回答

0

试试这个

Change content of Image and of TextBlock when the Image and the TextBlock are in a button我们使用资源的按钮,我们在这里如果你要定义一个`MyButton`自定义类的`Button`使用<ui:MyButton>

<ui:MyButton Margin="10" Content="{Binding MyName}" Tag="{Binding Path}" HorizontalAlignment="Center" VerticalAlignment="Top"> 
    <ui:MyButton.Resources> 
     <BitmapImage x:Key="Bmp" UriSource="/Images/arrowRight.png"></BitmapImage> 
    </ui:MyButton.Resources> 
</ui:MyButton> 
+0

它不起作用。 –

+0

它的工作here.i在这里使用您的代码checked.check您的图像源。 –

+0

@petko_stankoski请在StackOverflow中传达您的问题时特别注意。 *“不起作用”*是完全模糊的,不明确的,并且不会帮助人们理解您的问题。请尽可能详细地解释您目前的状况,并解释您的预期结果。 –