【发布时间】:2014-07-07 21:28:57
【问题描述】:
这是我的按钮代码:
<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"/>
一切看起来都像我想要的那样:第一张图片没有显示,按钮的第三部分(第二张图片)右对齐。
但是,这里:
<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>
当我这样调用按钮时,不会显示文本和第二张图片。显示的只是第一张图片,它通过整个按钮的宽度进行拉伸。为什么会这样?我想让它表现得像另一个。
【问题讨论】:
-
如果您要为您的
Button定义一个MyButton自定义类,那么只需将适当的DependencyPropertys 添加到它以绑定到您的两个ImageSource属性。 -
@Sheridan 我没有为按钮定义一个类。
-
@Sheridan 有一个类,但那是扩展 Button 类的类。没有别的了。没有定义属性。
-
天啊! 如果您要为您的
Button定义一个MyButton自定义类(您已经完成了),那么只需在其中添加适当的DependencyPropertys 以绑定到您的两个ImageSource属性。 这就是解决方案...只需将DependencyPropertys 添加到您的扩展类并绑定到它们。 -
@Sheridan LOL...我没有创建课程!我刚刚创建了一个类,但它派生自
Button,它没有任何属性!!你不得不承认这很搞笑......