【问题标题】:Auto size Button Style自动大小按钮样式
【发布时间】:2014-11-29 17:48:00
【问题描述】:

我有这个Style

<!--Normal Button Style -->
<Style TargetType="{x:Type Button}" x:Key="NormalButtonStyle">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ButtonBase}">
                <Grid Background="{TemplateBinding Background}" SnapsToDevicePixels="True" Effect="{DynamicResource ShadowEffect}" >
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="16"/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>

                    <Image Source="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Tag}" HorizontalAlignment="Left" Width="16" Stretch="None"  Margin="0,1" />
                    <!--<TextBlock Grid.Column="1" Text="{TemplateBinding Content}" Width="Auto" Margin="0,1" Padding="0" TextWrapping="Wrap" VerticalAlignment="Center" />-->         
                    <ContentPresenter Grid.Column="1" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" 
                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}" RecognizesAccessKey="True" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

还有这个Button

<Button Content="Record" HorizontalAlignment="Left" 
    VerticalAlignment="Top" Width="63" Style="{StaticResource NormalButtonStyle}" 
    Tag="Blabla.png" Height="24"/>

这给了我这个:

问题

出于本地化原因,如何根据内部文本使按钮具有自动大小?

【问题讨论】:

    标签: wpf xaml autosize wpf-style


    【解决方案1】:

    您的代码也可以正常工作。您只需从按钮中删除“Width=63”即可。

    另一种方法 这里我使用 stackpanel 作为基于内容的堆栈面板大小,并忽略有多少可用空间,如果有额外空间可用,则在此处使用 TextWrapping。

     <Window.Resources>
        <Style TargetType="{x:Type Button}" x:Key="NormalButtonStyle">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ButtonBase}">
                        <StackPanel MinHeight="{TemplateBinding MinHeight}" Orientation="Horizontal" Background="{TemplateBinding Background}" SnapsToDevicePixels="True" Effect="{DynamicResource ShadowEffect}" >
                            <Image VerticalAlignment="Center" HorizontalAlignment="Center" Source="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Tag}" Margin="2,0,2,0"  Width="16" Height="16" Stretch="None"/>
                            <TextBlock MaxWidth="{Binding Path=ActualWidth,RelativeSource={RelativeSource TemplatedParent}}"  HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Text="{TemplateBinding Content}" TextWrapping="Wrap" />
                        </StackPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid ShowGridLines="True" Width="500">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="100"/>            
        </Grid.RowDefinitions>
        <Button Content="R" HorizontalAlignment="Left" MinHeight="30" Padding="2,0,5,0"  Style="{StaticResource NormalButtonStyle}" Tag="btn1.jpg"/>
        <Button Content="Record" HorizontalAlignment="Left" Grid.Row="1" MinHeight="30" Padding="2,0,5,0"  Style="{StaticResource NormalButtonStyle}" Tag="btn1.jpg"/>
        <Button Content="Record here" HorizontalAlignment="Left" Grid.Row="2" MinHeight="30" Padding="2,0,5,0"  Style="{StaticResource NormalButtonStyle}" Tag="btn1.jpg"/>
        <Button Content="Record record record record Record record record record Record record record record Record record record record Record record record record Record record record record" Grid.Row="3" MinHeight="30" Padding="2,0,5,0"  Style="{StaticResource NormalButtonStyle}" Tag="btn1.jpg"/>
        <Button Content="Record record record record Record record record record Record record record record Record record record record Record record record record Record record record record" Grid.Row="4" MinHeight="30" Padding="2,0,5,0"  Style="{StaticResource NormalButtonStyle}" Tag="btn1.jpg"/>
    </Grid>
    

    注意

    1. 在样式中使用 MinHeight / MinWidth / MaxHeight / MaxWidth 属性 design.it 有助于本地化。

    2. 使用自动调整网格进行设计。

    结果

    【讨论】:

    • 由于我不知道谁先回答,accept 发给你,因为方法不同。
    【解决方案2】:

    简单:不要给按钮固定宽度(您已将其设置为63)并确保其HorizontalAlignment 未设置为Stretch(所以Left 很好)。

    您可能还想添加一些填充以给文本更多的喘息空间。

    【讨论】:

    • 哦,Width="63" 是我忘记删除的另一种方法的测试代码。哦,男孩... :(
    猜你喜欢
    • 2012-08-19
    • 2018-03-15
    • 2019-11-20
    • 2019-05-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-10
    • 1970-01-01
    相关资源
    最近更新 更多