在按钮控件的所有实例中,在按钮控件中设置不同的图像高度

时间:2014-10-27 08:15:36

标签: wpf button user-controls

我有一个看起来像这样的wpf控件:

<Button x:Class="myProject.UI.Controls.MyButton" FocusVisualStyle="{x:Null}" HorizontalContentAlignment="Center" VerticalContentAlignment="Center">
<Button.Template>
    <ControlTemplate TargetType="{x:Type Button}">
        <Border x:Name="border" CornerRadius="8" Background="{DynamicResource ResourceKey=BackgroundColor}">
            <DockPanel Margin="3">
                <Viewbox DockPanel.Dock="Left">
                    <Image Height="30" Source="{Binding Path=Tag, RelativeSource={RelativeSource TemplatedParent}}" Margin="5" VerticalAlignment="Center" HorizontalAlignment="Left" />
                </Viewbox>
                <TextBlock DockPanel.Dock="Left" Text="{Binding Path=Content,RelativeSource={RelativeSource TemplatedParent}}" FontSize="{Binding Path=FontSize,RelativeSource={RelativeSource TemplatedParent}}" VerticalAlignment="Center" Padding="10,0,10,0" HorizontalAlignment="Left"/>
                <Image DockPanel.Dock="Right" Source="{DynamicResource ResourceKey=Bmp}" Height="30" Width="30" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="0,0,10,0"></Image>
            </DockPanel>
        </Border>
    </ControlTemplate>
</Button.Template>

当我在我看来使用它时,这就是我写的:

<controls:MyButton FontSize="24" Width="210" Height="60" Foreground="White" Tag="{Binding Source={x:Static properties:Resources.backArrow}, Converter={StaticResource BitmapToImageSourceConverter}}" Content="{DynamicResource BackArrowButton}" Command="{Binding Path=GoToPreviousPage}" Margin="60 0 0 0">
    <Button.Resources>
        <Brush x:Key="BackgroundColor">#005BB5</Brush>
    </Button.Resources>
</controls:ImageButton>

问题是我的项目中有几个这个按钮的实例。在其中一些图像中,第一张图像(在视图框中)的高度应为30,其中一些图像的高度必须为10.

如何从按钮的每个实例中设置第一个图像的高度?例如,像这样:

<controls:MyButton **HeightOfTheFirstImage**="10" FontSize="24" Width="210" Height="60" Foreground="White" Tag="{Binding Source={x:Static properties:Resources.backArrow}, Converter={StaticResource BitmapToImageSourceConverter}}" Content="{DynamicResource BackArrowButton}" Command="{Binding Path=GoToPreviousPage}" Margin="60 0 0 0">
    <Button.Resources>
        <Brush x:Key="BackgroundColor">#005BB5</Brush>
    </Button.Resources>
</controls:ImageButton>

1 个答案:

答案 0 :(得分:0)

可以尝试将视图框高度设置为Min。身高......以及要拉伸的内容 http://msdn.microsoft.com/en-us/library/ms752301(v=vs.110).aspx

这可能会有所帮助。

相关问题